C# FileInfo.Extension 是否返回最后一个 *.* 模式,或其他什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12695502/
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
Does FileInfo.Extension return the last *.* pattern, or something else?
提问by Codeman
I'm curious what exactly the behavior is on the following:
我很好奇以下行为究竟是什么:
FileInfo info = new FileInfo("C:/testfile.txt.gz");
string ext = info.Extension;
Will this return ".txt.gz" or ".gz"?
这会返回“.txt.gz”还是“.gz”?
What is the behavior with even more extensions, such as ".txt.gz.zip" or something like that?
更多扩展名的行为是什么,例如“.txt.gz.zip”或类似的东西?
EDIT:
编辑:
To be clear, I've already tested this. I would like an explanation of the property.
需要明确的是,我已经对此进行了测试。我想要一个关于财产的解释。
采纳答案by Erwin
It will return .gz, but the explanation from MSDN (FileSystemInfo.Extension Property) isn't clear why:
它将返回 .gz,但 MSDN ( FileSystemInfo.Extension Property)的解释不清楚为什么:
"The Extension property returns the FileSystemInfo extension, including the period (.). For example, for a file c:\NewFile.txt, this property returns ".txt"."
" Extension 属性返回 FileSystemInfo 扩展名,包括句点 (.)。例如,对于文件 c:\NewFile.txt,此属性返回 ".txt"。"
So I looked up the code of the Extensionproperty with reflector:
于是我Extension用reflector查了一下属性的代码:
public string Extension
{
get
{
int length = this.FullPath.Length;
int startIndex = length;
while (--startIndex >= 0)
{
char ch = this.FullPath[startIndex];
if (ch == '.')
{
return this.FullPath.Substring(startIndex, length - startIndex);
}
if (((ch == Path.DirectorySeparatorChar) || (ch == Path.AltDirectorySeparatorChar)) || (ch == Path.VolumeSeparatorChar))
{
break;
}
}
return string.Empty;
}
}
It's check every char from the end of the filepath till it finds a dot, then a substring is returned from the dot to the end of the filepath.
它检查文件路径末尾的每个字符,直到找到一个点,然后从点到文件路径末尾返回一个子字符串。
回答by Thomas Levesque
It returns the extension from the last dot, because it can't guess whether another part of the filename is part of the extension. In the case of testfile.txt.gz, you could argue that the extension is .txt.gz, but what about System.Data.dll? Should the extension be .Data.dll? Probably not... There's no way to guess, so the Extensionproperty doesn't try to.
它从最后一个点返回扩展名,因为它无法猜测文件名的另一部分是否是扩展名的一部分。在 的情况下testfile.txt.gz,您可以争辩说扩展名是.txt.gz,但是呢System.Data.dll?扩展名应该是.Data.dll?可能不会... 无法猜测,因此该Extension物业不会尝试。
回答by Johan Larsson
[TestCase(@"C:/testfile.txt.gz", ".gz")]
[TestCase(@"C:/testfile.txt.gz.zip", ".zip")]
[TestCase(@"C:/testfile.txt.gz.SO.jpg", ".jpg")]
public void TestName(string fileName, string expected)
{
FileInfo info = new FileInfo(fileName);
string actual = info.Extension;
Assert.AreEqual(actual, expected);
}
All pass
全部通过
回答by Johan Larsson
The file extension starts at the last dot. Unfortunately, the documentation for FileSystemInfo.Extensiondoesn't answer that, but it logically must return the same value as Path.GetExtension, for which the documentation states:
文件扩展名从最后一个点开始。不幸的是,FileSystemInfo.Extension的文档没有回答这个问题,但它在逻辑上必须返回与Path.GetExtension相同的值,文档指出:
Remarks
The extension of path is obtained by searching path for a period (.), starting with the last character in path and continuing toward the start of path. If a period is found before a DirectorySeparatorChar or AltDirectorySeparatorChar character, the returned string contains the period and the characters after it; otherwise, Empty is returned.
For a list of common I/O tasks, see Common I/O Tasks.
评论
path 的扩展是通过在 path 中搜索句点 (.) 获得的,从 path 中的最后一个字符开始,一直到 path 的开头。如果在 DirectorySeparatorChar 或 AltDirectorySeparatorChar 字符之前找到句点,则返回的字符串包含句点及其后的字符;否则,返回 Empty。
有关常见 I/O 任务的列表,请参阅常见 I/O 任务。
It would be nice there is an authoritative answer on file names in general, but I'm having trouble finding it.
最好有一个关于文件名的权威答案,但我很难找到它。

