此文记录的是修改文件夹图片的工具类。

/***

    文件夹图标工具类

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    Date:   2024-01-15 15:18:00

    使用方法:
        FolderUtil.SetFolderIcon(dirPath, iconFilePath);

***/

namespace Lzhdim.LPF.Utility
{
    using System;
    using System.Runtime.InteropServices;

    /// <summary>
    /// 文件夹图标工具类
    /// </summary>
    public class FolderUtil
    {
        /// <summary>
        /// 设置文件夹图标
        /// </summary>
        /// <param name="dirPath">文件夹路径</param>
        /// <param name="strFile">图标文件路径</param>
        public static void SetFolderIcon(string dirPath, string filePath)
        {
            LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS();
            FolderSettings.dwMask = 0x10;
            FolderSettings.pszIconFile = filePath;
            FolderSettings.iIconIndex = 0;

            //UInt32 FCS_READ = 0x00000001;
            UInt32 FCS_FORCEWRITE = 0x00000002;
            UInt32 FCS_WRITE = FCS_FORCEWRITE;

            string pszPath = dirPath;
            UInt32 HRESULT = SHGetSetFolderCustomSettings(ref FolderSettings, pszPath, FCS_WRITE);
        }

        [DllImport("Shell32.dll", CharSet = CharSet.Auto)]
        private static extern UInt32 SHGetSetFolderCustomSettings(ref LPSHFOLDERCUSTOMSETTINGS pfcs, string pszPath, UInt32 dwReadWrite);

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        private struct LPSHFOLDERCUSTOMSETTINGS
        {
            public UInt32 dwSize;
            public UInt32 dwMask;
            public IntPtr pvid;
            public string pszWebViewTemplate;
            public UInt32 cchWebViewTemplate;
            public string pszWebViewTemplateVersion;
            public string pszInfoTip;
            public UInt32 cchInfoTip;
            public IntPtr pclsid;
            public UInt32 dwFlags;
            public string pszIconFile;
            public UInt32 cchIconFile;
            public int iIconIndex;
            public string pszLogo;
            public UInt32 cchLogo;
        }
    }
}