tags:

views:

44

answers:

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?

+1  A: 

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
@ Andrei well the files are hidden, and read only and it doesn't seem to be working.
NightsEVil
EDIT: Worked PERFECTLY! thank you so much i just didn't put in the path correcty.
NightsEVil