How to quickly check if folder is empty (.NET)?

后端 未结 18 830
旧时难觅i
旧时难觅i 2020-12-02 08:22

I have to check, if directory on disk is empty. It means, that it does not contain any folders/files. I know, that there is a simple method. We get array of FileSystemInfo\'

相关标签:
18条回答
  • 2020-12-02 08:47

    Based in Brad Parks code:

        public static bool DirectoryIsEmpty(string path)
        {
            if (System.IO.Directory.GetFiles(path).Length > 0) return false;
    
            foreach (string dir in System.IO.Directory.GetDirectories(path))
                if (!DirectoryIsEmpty(dir)) return false;
    
            return true;
        }
    
    0 讨论(0)
  • 2020-12-02 08:50

    Thanks, everybody, for replies. I tried to use Directory.GetFiles() and Directory.GetDirectories() methods. Good news! The performance improved ~twice! 229 calls in 221ms. But also I hope, that it is possible to avoid enumeration of all items in the folder. Agree, that still the unnecessary job is executing. Don't you think so?

    After all investigations, I reached a conclusion, that under pure .NET further optimiation is impossible. I am going to play with WinAPI's FindFirstFile function. Hope it will help.

    0 讨论(0)
  • 2020-12-02 08:51

    Some time you might want to verify whether any files exist inside sub directories and ignore those empty sub directories; in this case you can used method below:

    public bool isDirectoryContainFiles(string path) {
        if (!Directory.Exists(path)) return false;
        return Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).Any();
    }
    
    0 讨论(0)
  • 2020-12-02 08:54
    private static void test()
    {
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();
    
        string [] dirs = System.IO.Directory.GetDirectories("C:\\Test\\");
        string[] files = System.IO.Directory.GetFiles("C:\\Test\\");
    
        if (dirs.Length == 0 && files.Length == 0)
            Console.WriteLine("Empty");
        else
            Console.WriteLine("Not Empty");
    
        sw.Stop();
        Console.WriteLine(sw.ElapsedMilliseconds);
    }
    

    This quick test came back in 2 milliseconds for the folder when empty and when containing subfolders & files (5 folders with 5 files in each)

    0 讨论(0)
  • 2020-12-02 08:55

    I use this for folders and files (don't know if it's optimal)

        if(Directory.GetFileSystemEntries(path).Length == 0)
    
    0 讨论(0)
  • 2020-12-02 08:56

    I'm sure the other answers are faster, and your question asked for whether or not a folder contained files or folders... but I'd think most of the time people would consider a directory empty if it contains no files. ie It's still "empty" to me if it contains empty subdirectories... this may not fit for your usage, but may for others!

      public bool DirectoryIsEmpty(string path)
      {
        int fileCount = Directory.GetFiles(path).Length;
        if (fileCount > 0)
        {
            return false;
        }
    
        string[] dirs = Directory.GetDirectories(path);
        foreach (string dir in dirs)
        {
          if (! DirectoryIsEmpty(dir))
          {
            return false;
          }
        }
    
        return true;
      }
    
    0 讨论(0)
提交回复
热议问题