Skip to main content

 none
Folder icon change issue - Windows application, C# RRS feed

  • Question

  • Hi,

    I am trying to change the folder icon in my windows (C#) application after some action on the folder, ie, after uploading folder to some other location.

    I want to show the folder with some mark like tick mark on the top.

    I did that using ini file creation and ico image.

    But i facing some problem here.

    Some problems are also discussed the the below URL 

    http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/a63b0d8b-82c0-4e15-b1d5-1c6bec1ee96f

    I am also facing the same problem like, it is not changing as-soon-as-possible.

    I tried by refreshing the cache but didn't get any success. I have to do manual refresh many times, then only i can see the new folder icon.

    And this happens only in windows 7, it works beautifully in XP.

    another problem is, icon will not be shown if i change the folder view property to medium icons or large icons I can see the new icon only in small icon view and details view property.

    Can somebody please tell me if you have any solution for these issue.


    Thanks, Sharath
    • Moved by Leo Liu - MSFT Tuesday, January 10, 2012 9:00 AM Moved for better support. (From:Visual C# General)
    Saturday, January 7, 2012 2:38 PM

Answers

  • You need to create a multi size icon in one icon file.
    So that when we use different view to view the folder or applications, it has difference size icon for displaying.
    Or you can just use others tools to create this special icon file.
    And when we use the SHGetSetFolderCustomSettings function will let you do your job immediately and successfully.
        class Program
        {
            static void Main(string[] args)
            {
                LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS();
                FolderSettings.dwMask = 0x10;
                FolderSettings.pszIconFile = @"D:\My Pictures\testImg\IconPack\ICO\BlankFolder.ico";
                FolderSettings.iIconIndex = 0;
    
                UInt32 FCS_READ = 0x00000001;
                UInt32 FCS_FORCEWRITE = 0x00000002;
                UInt32 FCS_WRITE = FCS_READ | FCS_FORCEWRITE;
    
                string pszPath = @"G:\FreeZone\New folder\New folder";
                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;
            }
        }
    
    Enjoy it! 


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us


    Wednesday, January 11, 2012 1:38 PM
    Moderator

All replies

  • You need to create a multi size icon in one icon file.
    So that when we use different view to view the folder or applications, it has difference size icon for displaying.
    Or you can just use others tools to create this special icon file.
    And when we use the SHGetSetFolderCustomSettings function will let you do your job immediately and successfully.
        class Program
        {
            static void Main(string[] args)
            {
                LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS();
                FolderSettings.dwMask = 0x10;
                FolderSettings.pszIconFile = @"D:\My Pictures\testImg\IconPack\ICO\BlankFolder.ico";
                FolderSettings.iIconIndex = 0;
    
                UInt32 FCS_READ = 0x00000001;
                UInt32 FCS_FORCEWRITE = 0x00000002;
                UInt32 FCS_WRITE = FCS_READ | FCS_FORCEWRITE;
    
                string pszPath = @"G:\FreeZone\New folder\New folder";
                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;
            }
        }
    
    Enjoy it! 


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us


    Wednesday, January 11, 2012 1:38 PM
    Moderator
  • I like your answer Mike, it works great actually.  But curious if there is a way to do a similar method for Files not folders.  I already go through and have done a lot of icon manipulation with how they look.  But I can not for the life of me figure out how to programmaticly change a specified File.  i have a sinking feeling it's staring me in the face.
    Friday, June 22, 2012 9:31 PM
  • Thanks!

    But I cannot find an API can change one file's icon as you required.

    I just find the document to set for a type of files's icon setting: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682212(v=vs.85).aspx 

    And as far as I know one executable file's icon is embed in the file, and I just can find some APIs get the icon info: http://msdn.microsoft.com/en-us/library/windows/desktop/ff468847(v=vs.85).aspx 

    And if you also want to find a way to do what you wanted, and I think this forum would be helpful to you: http://social.msdn.microsoft.com/Forums/en-US/windowsuidevelopment/threads


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    Monday, June 25, 2012 3:13 AM
    Moderator

  • --
    .
    --
    "OmniSquall" wrote in message news:4edbcb42-dad7-4675-80c8-bccefeeede20...
    >I like your answer Mike, it works great actually.  But curious if there is a way to do a similar method for Files not folders.  I already go through and have done a lot of icon manipulation with how they look.  But I can not for the life of me figure out how to programmaticly change a specified File.  i have a sinking feeling it's staring me in the face.
    Monday, June 25, 2012 8:27 AM