获取驱动器和目录图标:Icon.ExtractAssociatedIcon(文件路径)不工作? [英] Getting icons of Drives and Directories: Icon.ExtractAssociatedIcon(filePath) doesn't work?

查看:848
本文介绍了获取驱动器和目录图标:Icon.ExtractAssociatedIcon(文件路径)不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 Icon.ExtractAssociatedIcon(文件路径)的唯一办法<​​/ A >获取图标.NET和放大器;&安培;如果它不为驱动器和目录(是吗?)那你怎么去获得的图标呢?工作

If Icon.ExtractAssociatedIcon(filePath) is the only way to get icons in .NET && if it doesn't work for Drives and Directories (does it?) then how do you go about getting the icons for them?

换句话说,我想

foreach (string driveName in Directory.GetLogicalDrives())
//if (System.IO.Directory.Exists(driveName))
{
    using (System.Drawing.Icon systemIcon = System.Drawing.Icon.ExtractAssociatedIcon(driveName))
    { ... }
}

^这显然是行不通的(仅适用于文件的工作)

^ this obviously doesn't work (works only for files)

我不知道 System.IO.Directory.Exists(文件)是正确的方式进行检测,如果硬盘存在,以及...

I'm not sure System.IO.Directory.Exists(file) is the right way for detecting if drives exist as well...

推荐答案

要在.NET应用程序做到这一点,你将不得不P /调用的 的SHGetFileInfo 功能从Windows API(它定义于 SHELL32.DLL )。

To do this from a .NET application, you will have to P/Invoke the SHGetFileInfo function from the Windows API (it's defined in shell32.dll).

下面是一些示例code,显示如何做到这一点:获取相关的图标使用C#

Here is some sample code that shows how to do this: Getting Associated Icons Using C#

这篇关于获取驱动器和目录图标:Icon.ExtractAssociatedIcon(文件路径)不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆