好的,我已經看到了很多關於這個問題的問題,但仍然沒有人回答我的問題。實際上,我看到的每個問題都與另一個不同,這種訪問方式似乎確實讓程序員很煩惱。訪問路徑...被拒絕
請查看代碼:
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->屬性 - >安全 - >編輯 - >添加 - >鍵入每個人(在輸入對象名稱來選擇) - >確定 - >全控制給每個人手動更改權限。 (我也看到了我的用戶帳戶有完全控制權以及)
任何提示將深表讚賞
使用管理員角色試用此代碼.. – 2012-08-17 12:37:09
@raman:你能解釋一下嗎,你的意思是像添加清單,並以管理員身份運行應用程序? – vexe 2012-08-17 12:47:57
有兩個問題 - (a)您是否嘗試過使用Windows資源管理器手動創建這些子目錄(並執行此操作) - 然後您是否遇到任何問題?和(b)這段代碼是如何執行的?我的意思是,這段代碼是作爲exe的一部分(在它自己的進程中)運行,還是作爲dll的一部分(在某些主機進程中)運行,也許作爲一種服務(在這種情況下,帳戶是可配置的)? – PeteH 2012-08-17 12:52:25