Code Rewind
When its time to find, Hit Rewind
                 
 
 
Remember Me  
Recover Password
 
 Advanced Search
 
 Add to IE Search 
 
 
> Development > Application Development > Languages > .NET => How to get details of a file extension including icon
 

Available Files:
You must be signed in to download files.
You can Sign Up here if you are not a member.
Author: Administrator
Level: 3
Date Posted: 18-Nov-2008 (11:12)
Last Updated: 18-Nov-2008 (11:12)
Views: 1,250
Favorited: 0
Votes:
  -  1 votes
Your vote:
Click to rate Click to rate Click to rate Click to rate Click to rate
(5 Stars)
Rating: 5.00 out of 5


Summary: How to retrieve the descriptive name of a file extension and icon. 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.

Elaboration:
The information about file extensions is stored the registry in HKEY_CLASSES_ROOTS. If 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 ".doc" turns into DOC. 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();
 
    }

Version Tracking:

Version 1.0
 
KeyWords: Generic / None,C# (Generic / None);Generic / None (Generic / None);Generic / None (Generic / None);
CR Suggested .NET Articles
 
Multi-Threading in .NET by it2max (09-Aug-2007)
 
Most Recent Views
 
What happens to the .NET Code you write? by Guest (08-Sep-2010 03:15)
Using Trusted Connections & Impersonation in Web Applications by Guest (08-Sep-2010 03:07)
Windows Presentation Foundation (WPF) Part 2 by Guest (08-Sep-2010 02:39)
WPF Part 3 - Deploying WPF Applications by Guest (08-Sep-2010 02:35)
Windows Presentation Foundation (WPF) Part 1 by Guest (08-Sep-2010 02:28)
 
 Messages: 0, Topics: 0. Post New Message Please login to post a message...
  View
Items per page
Message since
  TOPIC
AUTHOR
VIEWS
REPLIES
LAST POST
No messages boards...

Post New Message      
General Comment News / Info Question Answer Joke / Game Admin Answer
SEARCH ON FORUM
 
 
   
 
 
TOP USERS
 
No top users
 
 
TOP DISCUSSIONS
 
No popular discussions
 
 
SPONSORED ADS
 
 
 
 



 
Registered Members: 1621
Now Browsing: 5
 
Subscribe to newsletter
 
 

 
TheBusinessXP
IT2Max
AtHomeTution
RapidConvert
 








 
About Us  |  Contact Us   |  Privacy Policy  |  Legal Notice  |  Terms and Conditions  |  Help   |  Browse CR   |  Articles  |  Webcasts  |  Ask an Expert   |  Message Boards   |  Downloads  |  Open Arena   |  FAQ | DaniWeb | GetAHelpdesk | ProgTalk
Copyright © 2007 CodeRewind.com. All rights reserved
designed by IT2Max, INC.
 
Execution Time: 3.68 sec