views:

153

answers:

2

In classic 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.

+3  A: 

I think it has been answered here:

http://stackoverflow.com/questions/1437382/get-file-type-in-c

Vladimir Georgiev
Ah, I missed that one since I was focused on it being part of the FileSystemObject and assumed that aspect would be mentioned. I'd prefer to avoid the PInvoke but it looks like I might have to. Thanks.
John
+1  A: 

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.

0xA3
I was hoping to avoid reading the values manually but given the options I may may go this route
John