How to Delete a entire folder and all its contents including readonly files
i currently use this code to delete a folder and its contents:
string tempFolder = System.Environment.GetEnvironmentVariable("HomeDrive");
System.IO.Directory.Delete(tempFolder + "\\" + "Test", true);
and it works GREAT but, it will delete the folder and its contents but, will NOT delete read only files. So how using c# 开发者_如何学编程targeted Framework of 2.0 can i accomplish this?
You can remove the read-only attribute from the files using the following code:
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);
}
精彩评论