开发者

Fastest way to count folder files in .NET 4.0

I need to count the number of files in a folder in .NET 4: The count will retu开发者_如何学编程rn number of all files except the .db file in the folder.

Option 1:

 IEnumerable<string> enumerables = Directory.EnumerateFiles(strPath, "*.*", SearchOption.TopDirectoryOnly);
 int iNumFiles = 0; 
 foreach (string f in enumerables)
 {
    if (!f.EndsWith(".db"))
       iNumFiles++;
 }

 //iNumFiles is the count

Option 2:

int iNumFiles = 0; 
IEnumerable<string> enumerables1 = Directory.EnumerateFiles(strPath, "*.*", SearchOption.TopDirectoryOnly);
IEnumerable<string> enumerables2 = Directory.EnumerateFiles(strPath, "*.db", SearchOption.TopDirectoryOnly);

iNumFiles = enumerables1.Count() - enumerables2.Count();

//iNumFiles is the count

Is there any other simpler but better methods (using RegEx or something else) that I should use?

EDIT: Should I keep the .db file or how useful it is? All I know it is the database (cache) of folder contents.

This is messing up my file count.

Thanks for reading.


Turn the . results into a queryable object. then query out the *.db items.

var queryObject = My.Computer.FileSystem.getFiles(foldername, FileIO.SearchOption.SearchTopLevelOnly, "*.*").AsQueryable();

var filesIcareAbout = queryObject.where(f => right(f,3) != ".db");

I took some syntax shorcuts. This may not be exactly right syntactically.


new DirectoryInfo(strPath).EnumerateFiles("*.db").Count();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜