How to read File names recursively from subfolder using LINQ

前端 未结 5 1178
鱼传尺愫
鱼传尺愫 2021-01-13 09:36

How to read file name with dll extension from a directory and from its subfolders recursively using LINQ or LAMBDA expression.

Now i\'m using Nested for

相关标签:
5条回答
  • 2021-01-13 10:08

    Reading files and directories is usually done with classes situated in the System.IO namespace. So the first step would consist into getting all the files that you need to read using the Directory.EnumerateFiles method and then for each file that corresponds to your search criteria read the contents using for example the File.ReadAllBytes method.

    0 讨论(0)
  • 2021-01-13 10:09
    IEnumerable<string> filenames = Directory.GetFiles(searchDirectory, "*.dll",
                                                       SearchOption.AllDirectories)
                                             .Select(s => Path.GetFileName(s));
    

    Directory.GetFiles() returns the full path of files that match the specified search pattern in the specified directory. Select projects each element of fullpath sequence into a new form, only the filename.

    0 讨论(0)
  • 2021-01-13 10:21

    You don't need to use LINQ to do this - it's built into the framework:

    string[] files = Directory.GetFiles(directory, "*.dll",
                                        SearchOption.AllDirectories);
    

    or if you're using .NET 4:

    IEnumerable<string> files = Directory.EnumerateFiles(directory, "*.dll",
                                                        SearchOption.AllDirectories);
    

    To be honest, LINQ isn't great in terms of recursion. You'd probably want to write your own general-purpose recursive extension method. Given how often this sort of question is asked, I should really do that myself some time...

    0 讨论(0)
  • 2021-01-13 10:23

    this returns just file names+extensions:

    DirectoryInfo di = new DirectoryInfo(@"d:\somewhere\");
    var q = from i in di.GetFiles("*.dll", SearchOption.AllDirectories)
            select i.Name;
    

    this returns just file names without extensions:

    DirectoryInfo di = new DirectoryInfo(@"d:\somewhere\");
    var q = from i in di.GetFiles("*.dll", SearchOption.AllDirectories)
            select System.IO.Path.GetFileNameWithoutExtension(i.Name);
    
    0 讨论(0)
  • 2021-01-13 10:23

    If you really want to do it with a recursive lambda expression here you go:

    Action<string, List<string>> discoverFiles = null;
    
    discoverFiles = new Action<string, List<string>>((dir, list) =>
        {
            try
            {
                foreach (var subDir in Directory.GetDirectories(dir))
                    discoverFiles(string.Concat(subDir), list);
    
                foreach (var dllFile in Directory.GetFiles(dir, "*.dll"))
                {
                    var fileNameOnly = Path.GetFileName(dllFile);
                    if (!list.Contains(fileNameOnly))
                        list.Add(fileNameOnly);
                }
            }
            catch (IOException)
            {
                // decide what to do here
            }
        });
    
    // usage:
    var targetList = new List<string>();
    discoverFiles("c:\\MyDirectory", targetList);
    
    foreach (var item in targetList)
        Debug.WriteLine(item);
    

    Note: this is probably several times slower (and way harder to read/debug/maintain) than the previous answers, but it does not stop if there is an I/O exception somewhere.

    0 讨论(0)
提交回复
热议问题