简体   繁体   English

在C#.NET中使用操作系统的文件类型映像

[英]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. 如果知道所需图标的文件名,则可以使用ExtractAssociatedIcon函数来实现此目的。 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. 当ExtractAssociatedIcon与位图一起使用时,如果运行该应用程序的系统具有注册表设置,导致位图文件显示为缩略图,则可能会返回缩略图而不是图标。

One other thing I found was that this method only extracts 32x32pixel icons. 我发现的另一件事是,此方法仅提取32x32pixel图标。

See System.Drawing.Icon.ExtractAssociatedIcon (MSDN) for more how to use this method. 有关如何使用此方法的更多信息,请参见System.Drawing.Icon.ExtractAssociatedIcon(MSDN)


For a more robust icon extractor, I'd suggest you look at the OSIcon project on CodeProject. 对于更强大的图标提取器,建议您查看CodeProject上的OSIcon项目。
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 这是项目的链接: http : //www.codeproject.com/KB/system/OSIcon.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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