是否FileInfo.Extension返回最后*。*模式,还是其他什么东西? [英] Does FileInfo.Extension return the last *.* pattern, or something else?

查看:2181
本文介绍了是否FileInfo.Extension返回最后*。*模式,还是其他什么东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,究竟该行为是以下内容:

I'm curious what exactly the behavior is on the following:

FileInfo info = new FileInfo("C:/testfile.txt.gz");
string ext = info.Extension;



这是否会回归.txt.gz或。广州?

Will this return ".txt.gz" or ".gz"?

什么是更扩展的行为,如.txt.gz.zip或者类似的东西。

What is the behavior with even more extensions, such as ".txt.gz.zip" or something like that?

编辑?

要清楚,我已经测试过这一点。我想物业的解释。

To be clear, I've already tested this. I would like an explanation of the property.

推荐答案

它将返回。广州,而是从MSDN(解释的FileSystemInfo.Extension物业)目前尚不清楚为什么:

It will return .gz, but the explanation from MSDN (FileSystemInfo.Extension Property) isn't clear why:

的扩展属性返回FileSystemInfo扩展,包括句例如,对于一个文件c():\NewFile.txt,此属性返回.TXT

于是我抬起头,反射器扩展属性代码:

So I looked up the code of the Extension property with 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.

这篇关于是否FileInfo.Extension返回最后*。*模式,还是其他什么东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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