Tuesday, 30 October 2012

Get the Description of File Types from Registry programmatically


Get the Description of File Types from Registry programmatically

 

The following Code Snippet will help you to get the information about a particular file extension directly from the registry. It can be used to provide the user about the nature of the file type..

Here is the code snippet for that:


/// <summary>

/// Goes through registry to get the file type description for each extension

/// </summary>

/// <param name="theExtension"></param>

/// <returns></returns>

private string GetFileTypeString(String theExtension)
{
    // HKEY_CLASSES_ROOT //

    RegistryKey root = Registry.ClassesRoot;
    // something like '.csproj'

    RegistryKey openValue = root.OpenSubKey(theExtension);
    try
    {
        if (openValue != null)
        {
            // This will get the name of a key - something like 'VisualStudio.csproj.8.0'

            String val = openValue.GetValue("").ToString();
            // So we go there to get the value //

            RegistryKey typeVal = root.OpenSubKey(val);
            // something like 'Visual C# Project file'

            return typeVal.GetValue("").ToString();

        }
        else
        {
            // Sometimes its unrecognized //

            return theExtension;
        }
    }
    //Sometimes the default key value is Null...no big deal

    catch (Exception)
    {
        return theExtension;
    }

} 

No comments:

Post a comment

Thank You for Your Comments. We will get back to you soon.

back to top