G Arshiya
G Arshiya

Reputation: 95

How to delete Files,MainFolder and SubFolders

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

Answers (5)

Thulasiram
Thulasiram

Reputation: 8552

 new System.IO.DirectoryInfo("C:\Temp").Delete(true);

 //Or

 System.IO.Directory.Delete("C:\Temp", true);

Upvotes: 1

Illuminati
Illuminati

Reputation: 4629

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

Upvotes: 3

Andrea Pigazzini
Andrea Pigazzini

Reputation: 357

I'd go for a:

Directory.Delete(Path1, true)

that will delete folders and files contained.

Upvotes: 4

Kamyar
Kamyar

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

Emond
Emond

Reputation: 50712

Direcory.Delete(path, true);

See here

Upvotes: 12

Related Questions