获取驱动器和目录图标:Icon.ExtractAssociatedIcon(文件路径)不工作?驱动器、图标、路径、文件

2023-09-04 02:29:13 作者:梦醒时分。

如果 Icon.ExtractAssociatedIcon(文件路径)是的唯一办法获取图标.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#

 
精彩推荐
图片推荐