当我使用右键菜单手动删除文件时,它被删除了。
但是当我运行这段代码时:
string[] filePaths = Directory.GetFiles(@"c:\My cloud\VM Instances\");
foreach (string filePath in filePaths)
File.Delete(filePath);
我遇到了这个错误:
"A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll Access to the path 'c:\My cloud\VM Instances\i-7e3cbb19 (manish).cvm' is denied. The thread '' (0xb5c) has exited with code 0 (0x0). A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll 'cloud_sync.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled."
那我该怎么办呢?
最佳答案
您的代码运行的进程与交互式登录进程具有不同的权限。重要的提示是您得到一个 UnauthorizedAccessException
。
这是一个网站吗?如果是这样,则 ASPNET 工作进程在设计上默认具有非常有限的权限(远离该默认值将很危险)。
这是 Windows 服务吗?如果是这样,默认情况下它们也会以权限非常有限的用户身份运行。
关于c# - 从任何文件夹中删除所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10147029/