0

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.

John Lewin
  • 6,050
  • 4
  • 22
  • 20

1 Answers1

1

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.

Community
  • 1
  • 1
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316