C# Recurse Directories using Directory.GetFiles and search pattern

前端 未结 6 1848
时光说笑
时光说笑 2021-01-21 02:42

I want to find all excel files within a directory structure using recursion. The problem is, the search pattern used in Directory.GetFiles only allows a single extension at a t

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-21 03:31

    I think the second alternative is more efficient. Loop through every file with the following pattern: .xl, then narrow the list looking for specific endings.

    Something like:

    foreach (String f in Directory.GetFiles(path, "*.xl*", SearchOption.AllDirectories))
    {
        if (HasSomeExcelExtension(f))
            files .Add(f);
    }
    

    You could use the EndsWith method to check "f" against each extension, or extract the extension using the Path.GetExtension method and look it up in a hashtable containing the desired extensions.

    just my $.02 hth

提交回复
热议问题