1

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

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

2 回答 2

0

The problem is the way in which the recursive function calls are done: the main function private static void WalkDirectory(DirectoryInfo directory) deletes all files and is called every time (even while analysing a subdirectory). Here you have a fix to make this code work as you wish:

   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();
            }
         }
      }
   }
于 2013-06-24T18:38:49.407 回答
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();
            }
         }
      }
   }
于 2013-06-25T12:55:21.143 回答