以下方法调用返回给定目录中的所有xml文件.
FileInfo[] Files = Directory.GetFiles("*.xml");
Run Code Online (Sandbox Code Playgroud)
我想获取fileName不以"_update.xml"结尾的目录中的所有XML文件
例如....如果我有以下文件
ABC.xml
ABC2.xml
ABC3_update.xml
Run Code Online (Sandbox Code Playgroud)
然后我想要一个只会返回的电话:
ABC.xml
ABC2.xml
Run Code Online (Sandbox Code Playgroud)
这可能吗?
LBu*_*kin 13
I don't believe you can use search wildcards for that kind of exclusion. You can, however, filter the list of files after the fact. It's quite easy with LINQ. Although, if your directory is very large, this may result in a lot of processing of the file list in memory.
Try:
FileInfo[] files =
Directory.GetFiles("*.xml") // all XML files
.Where( fi => !fi.Name.EndsWith( "_update.xml", CurrentCultureIgnoreCase ) )
.ToArray();
Run Code Online (Sandbox Code Playgroud)
没有像这样的通配符映射,没有.你需要删除之后不想要的.
请注意,Directory.GetFiles("*.xml")它实际上被解释为匹配所有扩展名为开头的 文件xml,而不仅仅是等于xml- 方法的怪癖!因此,您可能必须以类似的方式排除其他文件.
在searchPattern中使用星号通配符时(例如,".txt"),匹配行为取决于指定文件扩展名的长度.文件扩展名正好为三个字符的searchPattern返回扩展名为三个或更多字符的文件,其中前三个字符与指定的文件扩展名匹配在searchPattern中.文件扩展名为一个,两个或多于三个字符的searchPattern只返回扩展名与searchPattern中指定的文件扩展名完全匹配的文件.当使用问号通配符时,此方法返回只有与指定文件扩展名匹配的文件.例如,给定目录中的两个文件"file1.txt"和"file1.txtother",搜索模式"file?.txt"仅返回第一个文件,而搜索模式"file .txt"返回两个文件.
| 归档时间: |
|
| 查看次数: |
3850 次 |
| 最近记录: |