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

我是研究僧i 提交于 2019-12-07 09:46:07

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/5164794/use-the-file-types-image-of-the-operating-system-in-c-sharp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!