在.NET Framework中慎用DirectoryInfo.GetFiles方法

.NET Framework中的DirectoryInfo.GetFiles方法,可以在一个文件夹下通过通配符找出符合条件的文件。

我们首先在文件夹C:DemoFolder下定义两个文件:demo.xls和demo.xlsx

然后我们新建一个.NET Framework控制台项目,然后在其Program类的Main方法中敲入如下代码:

class Program
{
    static void Main(string[] args)
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(@"C:DemoFolder");//寻找在C:DemoFolder文件夹下的文件

        var files = directoryInfo.GetFiles("*.xls");//寻找文件夹下的所有.xls文件

        foreach (var file in files)
        {
            Console.WriteLine(file.Name);//输出找到的每个文件名
        }

        Console.WriteLine("Press key to end...");
        Console.ReadKey();
    }
}

这段代码的本意是找出文件夹C:DemoFolder下的所有.xls文件,但是我们看看输出结果如下:

我们惊讶地发现,明明我们输入DirectoryInfo.GetFiles方法的参数是*.xls,但是现在.xlsx后缀的文件也被找出来了。这个问题的根本原因是.NET Framework中,DirectoryInfo.GetFiles方法的通配符遵循了一套特殊的匹配规则,导致了*.xls匹配.xlsx后缀也是成功的。

微软MSDN上对此的解释如下:

The following list shows the behavior of different lengths for the searchPattern parameter: 
"*.abc" returns files having an extension of.abc,.abcd,.abcde,.abcdef, and so on. 
"*.abcd" returns only files having an extension of.abcd. 
"*.abcde" returns only files having an extension of.abcde. 
"*.abcdef" returns only files having an extension of.abcdef. 

详情请见下面MSDN链接的文章:

DirectoryInfo.GetFiles Method

此外上面MSDN中也提到如果文件夹中的文件较多,推荐使用DirectoryInfo.EnumerateFiles方法效率更高,原因如下:

The EnumerateFiles and GetFiles methods differ as follows:

EnumerateFiles
When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

GetFiles
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.
If there are no files in the DirectoryInfo, this method returns an empty array.

但是我测试了下本文所述问题在.NET Core中是不存在的,我们新建一个.NET Core控制台项目,然后复制粘贴上面Program类Main方法中的代码到.NET Core控制台项目,执行结果如下:

可以看到.NET Core正确地找出了.xls后缀的文件,没有找出.xlsx后缀的文件,所以目前来看本文所述问题只存在于.NET Framework中的DirectoryInfo.GetFiles和DirectoryInfo.EnumerateFiles方法上。

所以在.NET Framework中如果我们要用DirectoryInfo.GetFiles方法找出.xls后缀的文件,最保险的方法应该是下面这样:

class Program
{
    static void Main(string[] args)
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(@"C:DemoFolder");//寻找在C:DemoFolder文件夹下的文件

        var files = directoryInfo.GetFiles().Where(f => f.Name.EndsWith(".xls")).ToArray();//寻找文件夹下的所有.xls文件

        foreach (var file in files)
        {
            Console.WriteLine(file.Name);//输出找到的每个文件名
        }

        Console.WriteLine("Press key to end...");
        Console.ReadKey();
    }
}

通过EndsWith方法可以准确找出以.xls后缀结尾的所有文件。

原文地址:https://www.cnblogs.com/OpenCoder/p/10601781.html