获取文件夹图标路径

时间:2014-10-05 18:02:31

标签: c# winforms

我有很多带自定义图标的文件夹(除了默认的Windows文件夹图标之外的任何图标) 我想找到与文件夹目录关联的图标路径。

例如,我有一个名为&#34的文件夹; Documents"在不同的目录路径中有自定义图标。假设我只有文件夹目录路径,我想找到图标的路径。

d:\...\customicon.ico  (icon's path)
d:\...\Documents (folder's path)

下面是我想要的签名。

string getIconPath(string folderPath) {
    //return associated icon's path
}

2 个答案:

答案 0 :(得分:1)

这是我想要的功能:

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类实现:

[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);
}

答案 1 :(得分:0)

我不确定,一旦将图标设置为目录,原始图像路径信息就会保存在某处和/或某种程度上。 如果要从目录中提取图标,请查看此SO post和此CodeProject article

MSDN article显示如何从目录中提取图标。一种可能的解决方案是从目录中提取关联的图标并将其保存在所需名称下的目录中。这样,您将至少将图标作为所需目录中的文件(如果有帮助)。

相关问题