Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

c# - Folder Icon Change

I am changing the folder icons with c# function. Its working fine but the problem is its working for first time. I mean I am not able to change the icon for the folder for which I have changed the icon already. Here is the code below:

static void Main(string[] args)
{
    LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS();
    FolderSettings.dwMask = 0x10;
    FolderSettings.pszIconFile = @"C:Program Files (x86)Common FilesTortoiseOverlaysiconsXPStyleModifiedIcon.ico";
    FolderSettings.iIconIndex = 0;

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

    string pszPath = @"D:Downloaded Data";
    UInt32 HRESULT = SHGetSetFolderCustomSettings(ref FolderSettings, pszPath, FCS_WRITE);
    //Console.WriteLine(HRESULT.ToString("x"));
    //Console.ReadLine();

}

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

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
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;
}

What could be the reason?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've faced with similar issue. Just remove the desktop.ini file before calling the function for the second time. The same scenario is used if you would like to clear folder icon:

  1. Remove desktop.ini.
  2. Omit the following lines:

....

FolderSettings.pszIconFile = @"{icon path}";
FolderSettings.iIconIndex = 0;

....


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...