zoukankan      html  css  js  c++  java
  • 在.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后缀结尾的所有文件。

  • 相关阅读:
    php二维数组指定下标排序
    laravel使用auth管理后台amdin数据表
    laravel插件
    laravel中Horizon简单介绍适合于redis操作队列
    laravel5.5或laravel5.7版本自定义日志记录
    laravel使用"tymon/jwt-auth": "0.5.*"
    larval5.7安装jwt使用
    ubuntu ibus 输入法总在左下角不跟随光标的处理
    Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier
    30种mysql优化sql语句查询的方法<转>
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/10601781.html
Copyright © 2011-2022 走看看