问题描述
在这里的第一个问题。我正在开发的C#(.NET 3.5),在列表视图中显示文件的程序。我想有大图标视图中显示Windows资源管理器将使用该文件类型的图标,否则我将不得不使用一些现有的代码是这样的:
first question here. I'm developing a program in C# (.NET 3.5) that displays files in a listview. I'd like to have the "large icon" view display the icon that Windows Explorer uses for that filetype, otherwise I'll have to use some existing code like this:
private int getFileTypeIconIndex(string fileName)
{
string fileLocation = Application.StartupPath + "\\Quarantine\\" + fileName;
FileInfo fi = new FileInfo(fileLocation);
switch (fi.Extension)
{
case ".pdf":
return 1;
case ".doc": case ".docx": case ".docm": case ".dotx":case ".dotm": case ".dot":case ".wpd": case ".wps":
return 2;
default:
return 0;
}
}
上面的代码返回一个整数,用于选择从我与一些常见的图标填充的图像列表的图标。它工作正常,但我需要每个扩展在阳光下补充!有没有更好的办法?谢谢!
The above code returns an integer that is used to select an icon from an imagelist that I populated with some common icons. It works fine but I'd need to add every extension under the sun! Is there a better way? Thanks!
推荐答案
您可能会发现使用的一个更简单的(一个管理)比使用的SHGetFileInfo方法。但是要注意:两个文件具有相同的扩展名可能有不同的图标
You might find the use of Icon.ExtractAssociatedIcon a much simpler (an managed) approach than using SHGetFileInfo. But watch out: two files with the same extension may have different icons.
这篇关于我怎样才能获得的文件类型图标的Windows资源管理器显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!