2010-07-14 46 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

相关问题