2011-05-26 207 views
4

我在删除目录中的文件,主文件夹和子文件夹时遇到问题。我想在工作完成后删除所有文件,主文件夹和子文件夹。我正在使用下面的代码。如何删除文件,主文件夹和子文件夹

 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); 
     } 

如果有人愿意帮助我,那对我来说会很好。 由于事先

+2

“我有一个问题” - 那会是什么? – 2011-05-26 07:49:17

+0

你得到这个代码的任何错误。 – 2011-05-26 07:49:44

+0

不,它只能删除文件夹中的文件而不是文件夹和子文件夹 – 2011-05-26 07:50:58

回答

12
Direcory.Delete(path, true); 

See here

+0

哈!不知道那个!谢谢。 +1 – Kamyar 2011-05-26 07:53:08

0
using System.IO; 
private void EmptyFolder(DirectoryInfo directoryInfo) 
{ 
    foreach (FileInfo file in directoryInfo.GetFiles()) 
    { 
    file.Delete(); 
    } 
    foreach (DirectoryInfo subfolder in directoryInfo.GetDirectories()) 
    { 
    EmptyFolder(subfolder); 
    } 
} 

使用代码:

EmptyFolder(new DirectoryInfo(@"C:\yourPath")) 

here服用。

4

我会去一个:

Directory.Delete(Path1, true) 

,将删除文件夹和文件包含。

3

Directory.Delete(@"c:\test", true);会做

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

//Or 

System.IO.Directory.Delete("C:\Temp", true); 
相关问题