当先锋百科网

首页 1 2 3 4 5 6 7
string path ="";
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
if(folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
  path = folderBrowserDialog.SelectedPath;  //选择文件夹
}
if(path != "") //防止不选文件夹关闭对话框时出现异常
{
  if(Directory.GetDirectories(path).Length != )  //若文件夹下存在子文件夹
  {
    foreach(string folder in Directory.GetDirectories(path))  //遍历所有子文件夹
    {
        foreach(string file in Directory.GetFiles(folder))  //遍历所有子文件夹中的子文件
        {

        }
    }
  }
  else  //若文件夹下不存在子文件夹
  {
     foreach(string file in Directory.GetFiles(path)) //直接遍历文件夹下的子文件
     {

      }
  }
}
//遍历文件夹删除文件夹及其下面的所有子文件
foreach(string folder in Directory.GetDirectories(path))  //遍历所有子文件夹
{
 if (Directory.Exists(filePath))
 {
  Directory.Delete(filePath, true);
 }
}

或
DirectoryInfo dir = new DirectoryInfo(path); 
if (dir.Exists)
{
  DirectoryInfo[] childs = dir.GetDirectories();
  foreach (DirectoryInfo child in childs)
  {
    child.Delete(true);
  }
   dir.Delete(true);
}