Reputation: 95
I am having a problem in deleting Files,MainFolder And SubFolders in a Directory. I want to delete all the Files,MainFolders and Subfolders after the work is finish . I am using this following code.
private void bgAtoZ_DoWork(object sender, DoWorkEventArgs e)
{
string Path1 = (string)(Application.StartupPath + "\\TEMP\\a-z\\test" + "\\" +name);
StreamReader reader1 = File.OpenText(Path1);
string str = reader1.ReadToEnd();
reader1.Close();
reader1.Dispose();
File.Delete(Path1);
}
If anyone Would help me it would be nice for me. Thanks In Advance
Upvotes: 4
Views: 3032
Reputation: 8552
new System.IO.DirectoryInfo("C:\Temp").Delete(true);
//Or
System.IO.Directory.Delete("C:\Temp", true);
Upvotes: 1
Reputation: 357
I'd go for a:
Directory.Delete(Path1, true)
that will delete folders and files contained.
Upvotes: 4
Reputation: 18797
using System.IO;
private void EmptyFolder(DirectoryInfo directoryInfo)
{
foreach (FileInfo file in directoryInfo.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo subfolder in directoryInfo.GetDirectories())
{
EmptyFolder(subfolder);
}
}
To use the code:
EmptyFolder(new DirectoryInfo(@"C:\yourPath"))
Taken from here.
Upvotes: 0