Elisabeth
Elisabeth

Reputation: 21216

Use the file types image of the operating system in C# .NET

Can I use somehow the filetype images from my operating system and display them in my application?

Upvotes: 4

Views: 1815

Answers (1)

Alex Essilfie
Alex Essilfie

Reputation: 12613

If you know the file name of the file whose icon you want, you can use the ExtractAssociatedIcon function for that purpose. e.g.

Icon ico = Icon.ExtractAssociatedIcon(@"C:\WINDOWS\system32\notepad.exe");

There is a catch to this method though. According to its documentation,

When ExtractAssociatedIcon is used with bitmaps, a thumbnail image may be returned instead of an icon if the system that is running the application has a registry setting that causes bitmap files to be shown as thumbnail images.

One other thing I found was that this method only extracts 32x32pixel icons.

See System.Drawing.Icon.ExtractAssociatedIcon (MSDN) for more how to use this method.


For a more robust icon extractor, I'd suggest you look at the OSIcon project on CodeProject.
It allows you to get the associated icon of any file using its file name or extension.
Here is a sample code for doing that:

OSIcon.WinAPI.Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
Icon icon = OSIcon.IconReader.GetFileIcon(".png", IconReader.IconSize.Large, false, ref shfi);

This is the link to the project: http://www.codeproject.com/KB/system/OSIcon.aspx

Upvotes: 2

Related Questions