Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the filetype icon that Windows Explorer shows?

Tags:

c#

winforms

first question here. I'm developing a program in C# (.NET 3.5) that displays files in a listview. I'd like to have the "large icon" view display the icon that Windows Explorer uses for that filetype, otherwise I'll have to use some existing code like this:

        private int getFileTypeIconIndex(string fileName)
    {
        string fileLocation = Application.StartupPath + "\\Quarantine\\" + fileName;
        FileInfo fi = new FileInfo(fileLocation);
        switch (fi.Extension)
        {
            case ".pdf":
                return 1;
            case ".doc": case ".docx": case ".docm": case ".dotx":case ".dotm": case ".dot":case ".wpd": case ".wps":
                return 2;
            default:
                return 0;
        }

    }

The above code returns an integer that is used to select an icon from an imagelist that I populated with some common icons. It works fine but I'd need to add every extension under the sun! Is there a better way? Thanks!

like image 301
Paul Beesley Avatar asked Sep 20 '08 12:09

Paul Beesley


People also ask

How do I show the icon in File Explorer?

Press Windows Key + E to open File Explorer. Click the View option in the menu bar at the top of the window. In the drop-down menu, select Extra large icons, Large icons, Medium Icons, Small icons, List, Details, Tiles, or Content to change to the view you want to see.

What is the little building icon in File Explorer?

In File Explorer, you'll see the University of Northern Colorado with the little blue building icon, and under that you will see your SharePoint Documents library (folder) that you are now syncing. Use this folder as you normally would any other folder or file on your computer or OneDrive.


1 Answers

You might find the use of Icon.ExtractAssociatedIcon a much simpler (an managed) approach than using SHGetFileInfo. But watch out: two files with the same extension may have different icons.

like image 143
Martin Plante Avatar answered Oct 23 '22 10:10

Martin Plante