windows C#:使用 Directory.GetFiles 获取固定长度的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/963279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: Using Directory.GetFiles to get files with fixed length
提问by Jason Ching
The directory 'C:\temp' has two files named 'GZ96A7005.tif' and 'GZ96A7005001.tif'. They have different length with the same extension. Now I run below code:
目录“C:\temp”有两个名为“GZ96A7005.tif”和“GZ96A7005001.tif”的文件。它们具有不同的长度和相同的扩展。现在我运行下面的代码:
string[] resultFileNames = Directory.GetFiles(@"C:\temp", "????????????.tif");
The 'resultFileNames' return two items 'c:\temp\GZ96A7005.tif' and 'c:\temp\GZ96A7005001.tif'. But the Window Search will work fine. This is why and how do I get I want?
“resultFileNames”返回两个项目“c:\temp\GZ96A7005.tif”和“c:\temp\GZ96A7005001.tif”。但是窗口搜索可以正常工作。这就是为什么以及如何获得我想要的?
回答by Matthew Flaschen
For Directory.GetFiles, ? signifies "Exactly zero orone character." On the other hand, you could use DirectoryInfo.GetFiles, for which ? signifies "Exactly one character" (apparently what you want).
对于Directory.GetFiles,?表示“正好零个或一个字符”。另一方面,您可以使用DirectoryInfo.GetFiles,对于哪个?表示“恰好一个字符”(显然是您想要的)。
EDIT:
编辑:
Full code:
完整代码:
string[] resultFileNames = (from fileInfo in new DirectoryInfo(@"C:\temp").GetFiles("????????????.tif") select fileInfo.Name).ToArray();
You can probably skip the ToArray and just let resultFileNames be an IEnumerable<string>
.
您可以跳过 ToArray 并让 resultFileNames 为IEnumerable<string>
.
People are reporting this doesn't work for them on MS .NET. The below exact code works for me with on Mono on Ubuntu Hardy. I agree it doesn't really make senseto have two related classes use different conventions. However, that is what the documentation (linked above) says, and Mono complies with the docs. If Microsoft's implementation doesn't, they have a bug:
人们报告这在 MS .NET 上对他们不起作用。下面的确切代码适用于我在 Ubuntu Hardy 上的 Mono 上。我同意让两个相关的类使用不同的约定真的没有意义。但是,这就是文档(上面链接)所说的,Mono 遵守文档。如果微软的实现没有,他们有一个错误:
using System;
using System.IO;
using System.Linq;
public class GetFiles
{
public static void Main()
{
string[] resultFileNames = (from fileInfo in new DirectoryInfo(@".").GetFiles("????????????.tif") select fileInfo.Name).ToArray();
foreach(string fileName in resultFileNames)
{
Console.WriteLine(fileName);
}
}
}
回答by Lucas
I know I've read about this somewhere before, but the best I could find right now was this reference to it in Raymond Chen's blog post. The point is that Windows keeps a short (8.3) filename for every file with a long filename, for backward compatibility, and filename wildcards are matched against both the long and short filenames. You can see these short filenames by opening a command prompt and running "dir /x
". Normally, getting a list of files which match ????????.tif
(8) returns a list of file with 8 or less characters in their filename and a .tif extension. But every file with a long filename also has a short filename with 8.3 characters, so they all match this filter.
我知道我以前在某处读到过这个,但我现在能找到的最好的就是Raymond Chen 的博客文章中对它的引用。关键是 Windows 为每个具有长文件名的文件保留一个短 (8.3) 文件名,以便向后兼容,并且文件名通配符与长文件名和短文件名匹配。您可以通过打开命令提示符并运行“ dir /x
”来查看这些短文件名。通常,获取匹配????????.tif
(8) 的文件列表会返回文件名中包含 8 个或更少字符且扩展名为 .tif 的文件列表。但是每个长文件名的文件也有一个 8.3 个字符的短文件名,所以它们都匹配这个过滤器。
In your case both GZ96A7005.tif
and GZ96A7005001.tif
are long filenames, so they both have a 8.3 short filename which matches ????????.tif
(anything with 8 or more ?
's).
在您的情况下,GZ96A7005.tif
和GZ96A7005001.tif
都是长文件名,因此它们都有一个匹配的 8.3 短文件名????????.tif
(任何具有 8 个或更多?
's 的)。
UPDATE... from MSDN:
更新...来自MSDN:
Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "
*1*.txt
" may return unexpected file names. For example, using a search pattern of "*1*.txt
" returns "longfilename.txt
" because the equivalent 8.3 file name format is "LONGFI~1.TXT
".
由于此方法检查具有 8.3 文件名格式和长文件名格式的文件名,类似于“
*1*.txt
”的搜索模式可能会返回意外的文件名。例如,使用“*1*.txt
”的搜索模式会返回“longfilename.txt
”,因为等效的 8.3 文件名格式是“LONGFI~1.TXT
”。
UPDATE: The MSDN docs specifiy different behavior for the "?
" wildcard in Directory.GetFiles() and DirectoryInfo.GetFiles(). The documentation seems to be wrong, however. See Matthew Flaschen's answer.
更新:MSDN 文档为?
Directory.GetFiles() 和 DirectoryInfo.GetFiles() 中的“ ”通配符指定了不同的行为。然而,文档似乎是错误的。参见Matthew Flaschen 的回答。
回答by Josh
The ? character matches "zero or one" characters... so from what you have I would imagine that your search pattern will match any file ending in ".tif" that is between zero and twelve characters long.
这 ?字符匹配“零个或一个”字符......所以根据您的情况,我可以想象您的搜索模式将匹配任何以“.tif”结尾且长度在零到十二个字符之间的文件。
Try dropping another file in that is only three characters long with a ".tif" extension and see if the code picks that up as well. I have a sneaking suspicion that it will ;)
尝试将另一个只有三个字符长且带有“.tif”扩展名的文件放入其中,看看代码是否也能接收到它。我有一个偷偷怀疑它会;)
As far as the Windows search is concerned, it is most definately not using the same algorithm under the hood. The ? character might have a very different meaning there than it does in the .Net search pattern specification for the Directory.GetFiles(string, string) method.
就 Windows 搜索而言,它绝对不是在幕后使用相同的算法。这 ?字符在此处的含义可能与 Directory.GetFiles(string, string) 方法的 .Net 搜索模式规范中的含义大不相同。
回答by Jason Ching
string path = "C:/";
var files = Directory.GetFiles(path)
.Where(f => f.Replace(path, "").Length == 8);
A little costly with the string replacement. You can add whatever extension you need.
更换字符串有点贵。您可以添加您需要的任何扩展。