2010-07-14 61 views
1

我目前使用此代碼來刪除文件夾及其內容:如何刪除整個文件夾及其所有內容,包括只讀文件

string tempFolder = System.Environment.GetEnvironmentVariable("HomeDrive"); 
System.IO.Directory.Delete(tempFolder + "\\" + "Test", true); 

和它的偉大工程,但是,它會刪除該文件夾及其內容但是,不會刪除只讀文件。那麼如何使用C#針對2.0的框架,我可以做到這一點?

+0

AFAIK system.io有一些方法來處理屬性,你用Google搜索這個? http://www.codeguru.com/csharp/csharp/cs_syntax/anandctutorials/article.php/c5861 [如何刪除C#中的只讀文件的目錄?(HTTP的 – Luiscencio 2010-07-14 23:35:11

+1

可能重複:// stackoverflow.com/questions/611921/how-do-i-delete-a-directory-with-read-only-files-in-c) – 2010-07-14 23:55:22

+0

@坦率,似乎並不想與.NET 2.0 – NightsEVil 2010-07-15 01:53:35

回答

5

您可以刪除只讀的使用下面的代碼文件屬性:

string[] allFileNames = System.IO.Directory.GetFiles(tempFolder, "*.*", System.IO.SearchOption.AllDirectories); 
foreach (string filename in allFileNames) { 
    FileAttributes attr = File.GetAttributes(filename); 
    File.SetAttributes(filename, attr & ~FileAttributes.ReadOnly); 
} 
+0

工作@安德烈以及文件隱藏,只讀,它似乎並沒有工作。 – NightsEVil 2010-07-15 01:52:39

+0

編輯:非常完美!非常感謝你,我只是沒有把道路放在正確的位置。 – NightsEVil 2010-07-15 02:01:42

相關問題