本文介绍了获取文件夹图标路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有很多带有自定义图标的文件夹(除了默认的 Windows 文件夹图标之外的任何图标)我想找到与文件夹目录关联的图标路径.
I have many folders with custom icon ( any icon except default windows folder icon)I want to find path of icons associated to a folder directory.
例如,我有一个名为Documents"的文件夹,它在不同的目录路径中有一个自定义图标.假设我只有文件夹目录路径,我想找到图标的路径.
For example I have a folder named "Documents" that has a custom icon in a different directory path. assume that I just have the folder directory path and I want to find the icon's path.
d:\...\customicon.ico (icon's path)
d:\...\Documents (folder's path)
下面是我想要的签名.
string getIconPath(string folderPath) {
//return associated icon's path
}
推荐答案
这是我想要的功能:
string getIconPath(string folderPath) {
SHFILEINFO shinfo = new SHFILEINFO();
Win32.SHGetFileInfo(folderPath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), (int)0x1000);
return shinfo.szDisplayName
}
这里是 SHFILEINFO 结构和 Win32 类实现:
and here are the SHFILEINFO struct and Win32 class implementations:
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
class Win32
{
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
}
这篇关于获取文件夹图标路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!