开发者

What's the .Net replacement for File.Type FileSystemObject property?

In cla开发者_JS百科ssic Asp we used the File.Type property to get the friendly name associated with a file type from the registry (e.g. "Text Document" for ".txt"). The FileInfo class which generally replaces the old COM object doesn't replicate this feature and so far I'm not having much success in my search of a replacement.


I'm not aware of a method in the BCL, but you could easily read it from the Registry:

using System;
using Microsoft.Win32;

class Program
{
    static void Main(string[] args)
    {

        string extension = ".txt";
        string nicename = "";

        using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(extension))
        {
            if (key != null)
            {
                string filetype = key.GetValue(null) as string;
                using (RegistryKey keyType = Registry.ClassesRoot.OpenSubKey(filetype))
                {
                    if (keyType != null)
                    {
                        nicename = keyType.GetValue(null) as string;
                    }
                }
            }
        }
        Console.WriteLine(nicename);

    }
}

However, the method used in the link provided by Vladimir is to be preferred as it uses an API interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜