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"?
What is the behavior with even more extensions, such as ".txt.gz.zip" or something like that?
EDIT:
To be clear, I've already tested this. I would like an explanation of the property.
It will return .gz, but the explanation from MSDN (FileSystemInfo.Extension Property) isn't clear why:
"The Extension property returns the FileSystemInfo extension, including the period (.). For example, for a file c:\NewFile.txt, this property returns ".txt"."
So I looked up the code of the
Extension
property with reflector: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.
The file extension starts at the last dot. Unfortunately, the documentation for FileSystemInfo.Extension doesn't answer that, but it logically must return the same value as Path.GetExtension, for which the documentation states:
It would be nice there is an authoritative answer on file names in general, but I'm having trouble finding it.
All pass
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 aboutSystem.Data.dll
? Should the extension be.Data.dll
? Probably not... There's no way to guess, so theExtension
property doesn't try to.