简体   繁体   中英

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

我可以以某种方式使用操作系统中的文件类型图像并将其显示在应用程序中吗?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM