Change folder icon

I am changing the icons in a folder using the C # function. His work is great, but the problem is that he works for the first time. I mean, I cannot change the icon for the folder for which I have already changed the icon. Here is the code below:

static void Main(string[] args) { LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS(); FolderSettings.dwMask = 0x10; FolderSettings.pszIconFile = @"C:\Program Files (x86)\Common Files\TortoiseOverlays\icons\XPStyle\ModifiedIcon.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?

+4
source share
3 answers

I ran into a similar problem. Just delete the desktop.ini file before calling the function a second time. The same script is used if you want to clear the folder icon:

  • Delete desktop.ini.
  • Omit the following lines:

....

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

....

+4
source

Just to complete, this code looks fine, but the third parameter in the SHGetSetFolderCustomSettings call should be FCS_FORCEWRITE to change the settings if they are already present. (FCS_WRITE will set it ONLY if NOT values ​​are already present)

See the docs for this setting: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762199(v=vs.85).aspx

+1
source

Just change

 UInt32 FCS_WRITE = FCS_READ | FCS_FORCEWRITE; 

to

 UInt32 FCS_WRITE = FCS_FORCEWRITE; 

The next time you run FCS_WRITE = FCS_READ, it means he won’t write agian.

+1
source

Source: https://habr.com/ru/post/1396402/


All Articles