Checking if folder has files

前端 未结 6 1323
清歌不尽
清歌不尽 2021-02-10 09:58

I have program which writes to database which folders are full or empty. Now I\'m using

bool hasFiles=false;
(Directory.GetFiles(path).Length >0) ? hasFiles=         


        
6条回答
  •  温柔的废话
    2021-02-10 10:46

    If you are using .Net 4.0 have a look at the EnumerateFiles method. http://msdn.microsoft.com/en-us/library/dd413232(v=VS.100).aspx

    The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned; when you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

    This way not all the files are retrieved from the folder, if the enumerator has at least 1 file the folder is not empty

提交回复
热议问题