HOWTO: Get the descriptive name of a file extension (and icon)

This came up before, so I whipped up a quick example. If you have some sort of file "browse" Window that you want to show the file name, and the "File Type" field where it says something like "File Folder", "WinZip File" or "FLG File" – here is some simple code that does that for you.
 
The information about file extensions is in the registry in HKEY_CLASSES_ROOTS. The extension description exists, it will be under a sub-key of the same name. The "(Default)" value will contain the PROGID to lookup. When you look up the PROGID, in there – it will have the actual description.
 
If it does exist, like say ".zzz" – then Windows Explorer defaults to "ZZZ file" – so that’s what this method does too.
 
See the following C# code see this in action:
 

    private string GetFileTypeDescription(string fileName)

    {

        if (string.IsNullOrEmpty(fileName))

            throw new ArgumentException("Argument ‘fileName’ cannot be null or empty.");

 

        // Get the file extension in the form of ".doc" or ".xls"

        string extension = Path.GetExtension(fileName);

 

        // If there is no file extension, Windows Explorer just shows "File"

        if (string.IsNullOrEmpty(extension))

            return "File";

 

        // Get the upper case version, without the ".". So ".fxcop" turns

        // into FXCOP. This is used for unknown file types. This is how

        // Windows Explorer shows unknown file types

        string extensionNoDot = extension.Substring(1, extension.Length – 1).ToUpper();

 

        // Go look up the extension in HKEY_CLASSES_ROOT

        RegistryKey extensionKey = Registry.ClassesRoot.OpenSubKey(extension);

       

        // If this is null, the file extension isn’t registered, so just

        // return the extension in caps, without the . – and " file". So

        // somefile.pdb turns into "PDB file"

        if (extensionKey == null)

            return extensionNoDot + " file";

 

        // The root/default value for a registry sub-key should have the

        // ProgId of the application that is used to open the file – so

        // go try to get that value.

        string lookupProgId = extensionKey.GetValue("", string.Empty).ToString();

 

        // If there is no default value, there is no application associated

        // with the file extension.

        if (string.IsNullOrEmpty(extension))

            return extensionNoDot + " file"; ;

 

        // Go lookup the progid

        RegistryKey progIdKey = Registry.ClassesRoot.OpenSubKey(lookupProgId);

 

        // If the progid wasn’t found, then show the default value.

        if (string.IsNullOrEmpty(extension))

            return extensionNoDot + " file"; ;

 

        // If we got here, the root/default value of the progid key should

        // have the friendly name of the application, so return that.

        // But again, if there was an error or if it’s empty, it defaults

        // to the standard "EXT file" format.

        return progIdKey.GetValue("", extensionNoDot + " file").ToString();

 

    }

Posted in Uncategorized, WinForms

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 2 other followers

%d bloggers like this: