1

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
NightsEVil
  • 507
  • 2
  • 13
  • 21
  • AFAIK system.io has some methods to deal with attributes, did you googled this? http://www.codeguru.com/csharp/csharp/cs_syntax/anandctutorials/article.php/c5861 – Luiscencio Jul 14 '10 at 23:35
  • 1
    possible duplicate of [How do I delete a directory with read-only files in C#?](http://stackoverflow.com/questions/611921/how-do-i-delete-a-directory-with-read-only-files-in-c) – Frank Schwieterman Jul 14 '10 at 23:55
  • @ Frank, doesn't seem to want to work with .NET 2.0 – NightsEVil Jul 15 '10 at 01:53

1 Answers1

5

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);
}
Andrei
  • 119
  • 2