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

标签 c# .net wpf icons drives

如果 Icon.ExtractAssociatedIcon(filePath)the only way在 .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 应用程序执行此操作,您必须 P/Invoke SHGetFileInfo function来自 Windows API(它在 shell32.dll 中定义)。

下面是一些示例代码,展示了如何执行此操作:Getting Associated Icons Using C#

关于c# - 获取驱动器和目录的图标 : Icon. ExtractAssociatedIcon(filePath) 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5523116/

相关文章:

c# - DataGridView 使用对象列表过滤 BindingSource 作为 DataSource

c# - 从 ASP.NET Web API 层使用的方法中抛出或不抛出异常

c# - Windows 7 系统默认控件颜色列表

c# - 更改 ListView 中选定行的颜色

c# - 无法与装饰层上的项目交互

c# - 与委托(delegate)一起解释这段代码

c# - 使用 JSON 反序列化方法时 VS 2013 崩溃

c# - 如何将FTP文件移动到C#中的另一个目录?

.net - EF4 + MVVM - 在 ViewModel 中公开实体?

c# - 处理 "application not found"等特定 Win32 异常的最佳方法是什么?