Is there a faster way than this to find all the files in a directory and all sub directories?

前端 未结 16 1433
盖世英雄少女心
盖世英雄少女心 2020-11-29 17:49

I\'m writing a program that needs to search a directory and all its sub directories for files that have a certain extension. This is going to be used both on a local, and a

16条回答
  •  佛祖请我去吃肉
    2020-11-29 18:13

    In .net core you can do something like this below. It can search for all subdirectories recursively with good performance and ignoring paths without access. I also tried other methods found in

    https://www.codeproject.com/Articles/1383832/System-IO-Directory-Alternative-using-WinAPI

    public static IEnumerable ListFiles(string baseDir)
    {
        EnumerationOptions opt = new EnumerationOptions();
        opt.RecurseSubdirectories = true;
        opt.ReturnSpecialDirectories = false;
        //opt.AttributesToSkip = FileAttributes.Hidden | FileAttributes.System;
        opt.AttributesToSkip = 0;
        opt.IgnoreInaccessible = true;
    
        var tmp = Directory.EnumerateFileSystemEntries(baseDir, "*", opt);
        return tmp;
    }
    

提交回复
热议问题