获取驱动器和目录的图标:Icon.ExtractAssociatedIcon(filePath)不起作用?

如果Icon.ExtractAssociatedIcon(filePath)是在.NET &&中获取图标的唯一方法 ,如果它不适用于驱动器和目录(是吗?)那么你如何为它们获取图标?

换句话说,我想

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

^这显然不起作用(仅适用于文件)

我不确定System.IO.Directory.Exists(file)是检测驱动器是否存在的正确方法……

要从.NET应用程序执行此操作,您必须从Windows API P / Invoke SHGetFileInfo函数 (它在shell32.dll定义)。

下面是一些示例代码,说明如何执行此操作: 使用C#获取关联的图标