Getting icons of Drives and Directories: Icon.ExtractAssociatedIcon(filePath) doesn't work?

痴心易碎 提交于 2019-12-21 17:18:08

问题


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?

In other words, I'd like to

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)

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


回答1:


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).

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



来源:https://stackoverflow.com/questions/5523116/getting-icons-of-drives-and-directories-icon-extractassociatediconfilepath-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!