How to delete files, MainFolder and SubFolders

I had a problem with deleting files, MainFolder And SubFolders catalog. I want to delete all files, MainFolders Subfolders and after completion of the work. I am using the following code.

private void bgAtoZ_DoWork(object sender, DoWorkEventArgs e) { string Path1 = (string)(Application.StartupPath + "\\TEMP\\az\\test" + "\\" +name); StreamReader reader1 = File.OpenText(Path1); string str = reader1.ReadToEnd(); reader1.Close(); reader1.Dispose(); File.Delete(Path1); } 

If someone has helped me, I would be fine. Thanks in advance...

+4
source share
5 answers
 Direcory.Delete(path, true); 

See here

+12
source

I would go for:

 Directory.Delete(Path1, true) 

which will remove files and folders.

+4
source

Directory.Delete(@"c:\test", true); ", true); Directory.Delete(@"c:\test", true); I would do it

+3
source
  new System.IO.DirectoryInfo("C:\Temp").Delete(true); //Or System.IO.Directory.Delete("C:\Temp", true); ") Delete (true);.  new System.IO.DirectoryInfo("C:\Temp").Delete(true); //Or System.IO.Directory.Delete("C:\Temp", true); 
+1
source
 using System.IO; private void EmptyFolder(DirectoryInfo directoryInfo) { foreach (FileInfo file in directoryInfo.GetFiles()) { file.Delete(); } foreach (DirectoryInfo subfolder in directoryInfo.GetDirectories()) { EmptyFolder(subfolder); } } ) 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 .

0
source

All Articles