locked
Set icon for specific files programmatically RRS feed

  • Question

  • Hello,

    With C#, I created a file with a .ui extension and I I would like to set an icon for all files with .ui extension.
    Is it possible to do that with C# .NET?
    If so, do you have a good example to do that, please?

    For example, if I want to make an installer to install an app, how can I set an icon for files with .ui automatically? 

    Thanks.

    Tuesday, June 16, 2020 10:04 PM

All replies

  • Just set the .ico in registry at

    HKEY_CLASSES_ROOT\.ui\DefaultIcon

    See How to Assign a Custom Icon to a File Type

    Tuesday, June 16, 2020 10:59 PM
  • Hi speed780,

    Thank you for posting here.

    If you want to open a file of a certain type with an application and modify the icon of these files, you can refer to the following code.

    Consistent with Castorix31's thinking, I achieved this by modifying the registry.

            static void Main(string[] args)
            {
                SetAssociationWithExtension(".ui", ".ui", @"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe", "ui");
                
            }
            public static void SetAssociationWithExtension(string Extension, string KeyName, string OpenWith, string FileDescription)
            {
                RegistryKey baseKey = Registry.ClassesRoot.CreateSubKey(Extension);
                baseKey.SetValue("", KeyName);
    
                RegistryKey openMethod = Registry.ClassesRoot.CreateSubKey(KeyName);
                openMethod.SetValue("", FileDescription);
                openMethod.CreateSubKey("DefaultIcon").SetValue(@"", "\"" + OpenWith + "\",0");
                RegistryKey shell = openMethod.CreateSubKey("Shell");
                shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\"");
                shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\"");
                baseKey.Close();
                openMethod.Close();
                shell.Close();
            }

    It should be noted that due to permission issues, this code needs to be run with administrator permissions, and after the execution is complete, the computer needs to be restarted to take effect.

    After restarting, the .ui file will open by default in Visual Studio, and the icon will be modified to look like Visual Studio.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, June 17, 2020 5:28 AM
  • Installers have this built in. You should post this question to the forum of the installer software you're using if their docs don't help you out. Each one is different but they all allow you use to register a file type icon with the shell.

    Installshield

    WiX

    Windows Installer



    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 17, 2020 1:44 PM