删除目录中除文件夹及其内容之外的所有文件

时间:2013-06-24 18:05:33

标签: c# .net windows-mobile-6 compact-framework2.0

我正在尝试删除存储卡上的所有文件而不删除存储卡。我可以使用当前代码保留我指定的目录,但不保留其内容。它只留下空白 文件夹数据,因为它删除内部的一切如何防止它删除该文件夹及其内容?

private void button1_Click(object sender, EventArgs e)
{
   ScanDirectory scanDirectory = new ScanDirectory();
   scanDirectory.WalkDirectory(@"/Storage Card");
   scanDirectory.WalkDirectory(@"/Application");
}

public class ScanDirectory
{
   public void WalkDirectory(string directory)
   {
      WalkDirectory(new DirectoryInfo(directory));
   }

   private static void WalkDirectory(DirectoryInfo directory)
   {
      // Scan all files in the current path
      foreach (FileInfo file in directory.GetFiles())
      {
         file.Attributes &= ~FileAttributes.ReadOnly;

         var name = file.Name;
         name = name.ToLower();
         if (name != "test.txt")
         {
            file.Delete();
         }
      }
      DirectoryInfo[] subDirectories = directory.GetDirectories();
      foreach (DirectoryInfo subDirectory in subDirectories)
      {
         WalkDirectory(subDirectory);
         subDirectory.Attributes &= ~FileAttributes.ReadOnly;

         var name = subDirectory.Name;
         name = name.ToLower();
         if (name != "data")
         {
            subDirectory.Delete();
         }
      }
   }
}    

2 个答案:

答案 0 :(得分:0)

问题在于递归函数调用的完成方式:main函数private static void WalkDirectory(DirectoryInfo directory)删除所有文件并每次调用(即使在分析子目录时)。在这里你有一个修复程序,使这段代码按你的意愿工作:

   private static void WalkDirectory(DirectoryInfo directory)
   {
       if (directory.Name.ToLower() != "data")
       {
         // Scan all files in the current path
         foreach (FileInfo file in directory.GetFiles())
         {
             file.Attributes &= ~FileAttributes.ReadOnly;

             var name = file.Name;
             name = name.ToLower();
             if (name != "test.txt")
             {
               file.Delete();
             }
         }

         DirectoryInfo[] subDirectories = directory.GetDirectories();
         foreach (DirectoryInfo subDirectory in subDirectories)
         {
            WalkDirectory(subDirectory);
            subDirectory.Attributes &= ~FileAttributes.ReadOnly;

            var name = subDirectory.Name;
            name = name.ToLower();
            if (name != "data")
            {
               subDirectory.Delete();
            }
         }
      }
   }

答案 1 :(得分:0)

private static void WalkDirectory(DirectoryInfo directory)
   {
       if (directory.Name.ToLower() != "data")
       {
         // Scan all files in the current path
         foreach (FileInfo file in directory.GetFiles())
         {
             file.Attributes &= ~FileAttributes.ReadOnly;

             var name = file.Name;
             name = name.ToLower();
             if (name != "test.txt")
             {
               file.Delete();
             }
         }

         DirectoryInfo[] subDirectories = directory.GetDirectories();
         foreach (DirectoryInfo subDirectory in subDirectories)
         {
            WalkDirectory(subDirectory);
            subDirectory.Attributes &= ~FileAttributes.ReadOnly;

            var name = subDirectory.Name;
            name = name.ToLower();
            if (name != "data")
            {
               subDirectory.Delete();
            }
         }
      }
   }