title: How to quickly check if folder is empty (.NET)?
body: <p>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's and check if count of elements equals to zero. Something like that:</p>
  
  <pre><code>public static bool CheckFolderEmpty(string path)
  {
      if (string.IsNullOrEmpty(path))
      {
          throw new ArgumentNullException("path");
      }
  
      var folder = new DirectoryInfo(path);
      if (folder.Exists)
      {
          return folder.GetFileSystemInfos().Length == 0;
      }
  
      throw new DirectoryNotFoundException();
  }
  </code></pre>
  
  <p>This approach seems OK. BUT!! It is very, very bad from a perspective of performance. <strong>GetFileSystemInfos()</strong> is a very hard method. Actually, it enumerates all filesystem objects of folder, gets all their properties, creates objects, fills typed array etc. And all this just to simply check Length. That's stupid, isn't it?</p>
  
  <p>I just profiled such code and determined, that ~250 calls of such method are executed in ~500ms. This is very slow and I believe, that it is possible to do it much quicker.</p>
  
  <p>Any suggestions?</p>