Custom icon for ClickOnce application in 'Add or Remove Programs'
Asked Answered
V

2

14

A ClickOnce application created using Mage is not showing the icon that was specified in for the Mage command-line parameter in control panel Add or Remove Programs.

I read some blogs, like:

How can I achieve this without editing registry keys? Is it possible?

Venery answered 7/6, 2012 at 7:0 Comment(3)
Related: Icon for ClickOnce application in 'Add or Remove Programs'Saffron
Here's a step by step similar solution.Tricorn
go through this link resolve this issue #16205389Polygraph
L
16

There's no way to do this without editing the registry, but you can do it programmatically. You have to be sure the icon is included in the deployment. We set our assembly description to the same string as our Product Name, so we can look through the uninstall strings for the right application by searching for the assembly description. This way, we don't have to hardcode the product name in this code.

        private static void SetAddRemoveProgramsIcon()
    {
        //only run if deployed 
        if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed
             && ApplicationDeployment.CurrentDeployment.IsFirstRun)
        {
            try
            {
                Assembly code = Assembly.GetExecutingAssembly();
                AssemblyDescriptionAttribute asdescription =
                    (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(code, typeof(AssemblyDescriptionAttribute));
                string assemblyDescription = asdescription.Description;

                //the icon is included in this program
                string iconSourcePath = Path.Combine(System.Windows.Forms.Application.StartupPath, "youriconfile.ico");

                if (!File.Exists(iconSourcePath))
                    return;

                RegistryKey myUninstallKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");
                string[] mySubKeyNames = myUninstallKey.GetSubKeyNames();
                for (int i = 0; i < mySubKeyNames.Length; i++)
                {
                    RegistryKey myKey = myUninstallKey.OpenSubKey(mySubKeyNames[i], true);
                    object myValue = myKey.GetValue("DisplayName");
                    if (myValue != null && myValue.ToString() == assemblyDescription)
                    {
                        myKey.SetValue("DisplayIcon", iconSourcePath);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                //log an error
            }
        }
    }
Leannleanna answered 19/6, 2012 at 8:2 Comment(1)
Where do you run this from?Zebulon
M
0

.NET 8 and beyond

/// <summary>
/// .NET 8 and beyond
/// set the icon in add/remove programs for all "yourApp"
/// use "!DEBUG" not debug to avoid this when debug
/// You can call this in for example : pplication_Startup event (app.xaml.cs)
/// </summary>
private static void SetDisplayIcon()
{
    //only run in Release
    if (YourApp.Properties.Settings.Default.IsFirstRun)
    {
        try
        {
            // executable file
            var exePath = Environment.ProcessPath;
            if (!System.IO.File.Exists(exePath))
            {
                return;
            }

            //DisplayIcon == "dfshim.dll,2" => 
            var myUninstallKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");
            string[]? mySubKeyNames = myUninstallKey?.GetSubKeyNames();
            for (int i = 0; i < mySubKeyNames?.Length; i++)
            {
                RegistryKey? myKey = myUninstallKey?.OpenSubKey(mySubKeyNames[i], true);
                // ClickOnce(Publish)
                // Publish -> Settings -> Options 
                // Publish Options -> Description -> Product name (is your DisplayName)
                var displayName = (string?)myKey?.GetValue("DisplayName");
                if (displayName?.Contains("YourApp") == true)
                {
                    myKey?.SetValue("DisplayIcon", exePath + ",0");
                    break;
                }
            }
            YourApp.Properties.Settings.Default.IsFirstRun = false;
            YourApp.Properties.Settings.Default.Save();
        }
        catch { }
    }
}

Note: I'm using "Environment.ProcessPath" to get the application location.

Use the IsFirstRun to True on default Settings.

Munroe answered 11/1 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.