多和具体的一些推广C#的GetFiles [英] GetFiles with multiple and specific extentions c#

查看:419
本文介绍了多和具体的一些推广C#的GetFiles的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有Excel文件的扩展名是XLS和XLSX,从一个特定的目录FileInfo对象,所以我把下面的代码

I want to have Excel files whose extensions are xls and xlsx, with FileInfo object from a specific directory so I put the following code

    System.IO.FileInfo[] files = null;
        System.IO.DirectoryInfo dirInfo;

        dirInfo = new System.IO.DirectoryInfo(this.tbFolderTo.Text);

        string[] extensions = new[] { "*.xls", "*.xlsx" };
        List<string> _searchPatternList = new List<string>(extensions);            
        List<string> fileList = new List<string>();
        foreach (string ext in _searchPatternList)
        {
            foreach (string subFile in Directory.GetFiles(this.tbFolderTo.Text, ext))
            {
                fileList.Add(subFile);
            }
        }

        foreach (string file in fileList)
        {
            this.lbFileNamesTo.Items.Add(file);
        }



但像filexp2.xlsq或filexp.xlsa伪造文件检测问题,我看到在我的列表框中显示找到的文件列表中这些文件,在代码中,我限制扩展XLS和XLSX我不知道为什么我在结果

but the problem by testing with bogus files like filexp2.xlsq or filexp.xlsa , I see these files in my list box to display the list of found files, in code I limited extensions to xls and xlsx I don't know why i see these files in the result

,结果我看不到我puted的代码,该代码

with the result I do not see any difference between the code I puted and this code

        System.IO.FileInfo[] files = null;
        System.IO.DirectoryInfo dirInfo;

        dirInfo = new System.IO.DirectoryInfo(this.tbFolderTo.Text);
        files = dirInfo.GetFiles("*.xls*");



感谢您的帮助。

thanks for help

推荐答案

从MSDN:

当在使用是searchPattern星号通配符,如* .TXT当分机正是三个字符长的匹配行为是比当分机多于或少于三个字符长不同。与正好三个字符的文件扩展名的是searchPattern返回具有三个或更多个字符,其中前三个字符中的是searchPattern指定的文件扩展名匹配一个扩展的文件。一个,两个,或三个以上的字符的文件扩展名的是searchPattern仅返回具有匹配是searchPattern指定的文件扩展正是长度的扩展名的文件。当使用问号通配符,这种方法只返回匹配指定文件扩展名的文件。例如,给定的两个文件,FILE1.TXT和file1.txtother,在一个目录,一个搜索模式文件?的.txt返回刚刚第一个文件,而文件* .TXT的搜索模式返回这两个文件。

When using the asterisk wildcard character in a searchPattern, such as "*.txt", the matching behavior when the extension is exactly three characters long is different than when the extension is more or less than three characters long. A searchPattern with a file extension of exactly three characters returns files having an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. A searchPattern with a file extension of one, two, or more than three characters returns only files having extensions of exactly that length that match the file extension specified in the searchPattern. When using the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, while a search pattern of "file*.txt" returns both files.

HTTP ://msdn.microsoft.com/it-it/library/wz42302f.aspx

这篇关于多和具体的一些推广C#的GetFiles的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆