且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取文件夹图标路径

更新时间:2023-01-15 22:00:53

这是我想要的功能:

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