Possible Duplicate:
DirectoryInfo.Delete vs Directory.Delete
I made this code to empty some files that I regularly delete, such as temp and MSN cahes files in Windows.
code1
I can add new paths to apply DeleteContains method in easy way I just have to add the path to the list
code2
will not allow me to add paths as much I want and I have to create new array of string for each path and a new loop too
anyway to use code1 to delete contains of the folder NOT the folder and its contains??
any work around in the foreach in code1 to work as code2 ??
Because some Folder can be delete or deleting them will cause problem for some apps and the apps wont work again it like
"C:\Users\user\AppData\Local\Temp"
while other folder can be delete it with no problem like MSN cashes
"C:\Users\user\AppData\Local\Microsoft\Windows Live\Contacts"
code1
private void Clear_Click(object sender, EventArgs e)
{
List<DirectoryInfo> FolderToClear = new List<DirectoryInfo>();
// here is a list of files I want to delete
FolderToClear.Add(new DirectoryInfo("path1"));
FolderToClear.Add(new DirectoryInfo("path2"));
FolderToClear.Add(new DirectoryInfo("path3"));
FolderToClear.Add(new DirectoryInfo("path4"));
foreach (DirectoryInfo directory in FolderToClear)
{
directory.Delete(true);
}
}
code2
private void DeleteContents(string Path)
{
string[] DirectoryList = Directory.GetDirectories(Path1);
string[] FileList = Directory.GetFiles(Path1);
foreach (string xin FileList)
{
File.Delete(x);
}
foreach ( string x in DirectoryList)
{
Directory.Delete(x, true);
}
}