好吧,我已经看到很多关于这件事的问题,但仍然没有人回答我的问题。事实上,我看到的每一个问题都不尽相同,这个访问问题似乎真的在困扰程序员。
请查看代码:
DirectoryInfo Dir1 = Directory.CreateDirectory(Desktop + "\\DIR1");
DirectoryInfo Dir2 = Directory.CreateDirectory(Desktop + "\\DIR2");
//* Lets Create a couple of SubDirs in DIR1
for (int i = 0; i < 5; i++)
{
// this will create 5 SubDirs in DIR1, named Sub1, Sub2 ... Sub5.
Dir1.CreateSubdirectory("Sub" + (i + 1).ToString());
//* lets create 5 text files in each SubDir:
for (int j = 0; j < 5; j++)
{
File.Create(Dir1.FullName + "\\Sub"+(i+1).ToString() + "\\text"+(j+1).ToString() + ".txt");
}
}
//* Lets Move all what we created in DIR1 to DIR2 (THIS IS WHERE I'M GETTING THE EXCEPTION
Directory.Move(Dir1.FullName, Dir2.FullName + "\\DIR1");
// I also Tried Dir1.MoveTo(Dir2.FullName + "\\DIR1");
堆栈跟踪:
at System.IO.DirectoryInfo.MoveTo(String destDirName)
at Directory_Class.Program.Main(String[] args) in c:\users\vexe\documents\visual studio 2010\Projects\Directory_Class\Directory_Class\Program.cs:line 207
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
当然,我尝试了通常的做法:
DirectorySecurity DirSec = Dir1.GetAccessControl();
string user = Environment.UserName;
DirSec.ResetAccessRule(new FileSystemAccessRule(user, FileSystemRights.FullControl, AccessControlType.Allow));
Dir1.SetAccessControl(DirSec);
但是一点都没变!
我还尝试手动更改权限,方法是右键单击 dir1 -> 属性 -> 安全 -> 编辑 -> 添加 -> 键入每个人(在输入对象名称中选择) -> 确定 -> 对每个人的完全控制。(我还看到我的用户帐户也拥有完全控制权)
任何提示将不胜感激