2012-04-13 107 views
1

當我使用右鍵菜單手動刪除文件時,它將被刪除。刪除任何文件夾中的所有文件

但是當我運行這段代碼:

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."

那麼,應該怎麼辦?

回答

7

您的代碼運行的過程與您的交互式登錄過程具有不同的權限。最大的提示是你得到了一個UnauthorizedAccessException

這是一個網站嗎?如果是這樣,ASPNET工作進程在設計上默認具有非常有限的權限(並且遠離該缺省將是危險的)。

這是一個Windows的服務?如果是這樣,默認情況下,它們也以非常有限權限的用戶身份運行。

+0

非常感謝您的回覆。這是一個窗口服務。那麼,我如何才能以所有權限運行我的代碼? – manish 2012-04-13 19:28:35

+0

感謝您的回覆如此之快。這是一個窗口服務。那麼,我如何才能以所有權限運行我的代碼? – manish 2012-04-13 20:10:17

+0

@manish,考慮提出單獨的問題,或者更好地在搜索引擎上搜索「windows服務帳戶權限」(如http://www.bing.com/search?q=windows+service+account+permissions)。 – 2012-04-13 20:13:24

相關問題