ManjuVijayan
ManjuVijayan

Reputation: 348

Is there a way to change the icon of a ClickOnce application in 'Add or Remove Programs'?

I have one Windows application which is deployed using ClickOnce technology. Is there a way to change the icon of that application which is shown in the image?

Screenshot of the installer in action with a marker for the icon.

Upvotes: 10

Views: 8074

Answers (2)

icernos
icernos

Reputation: 405

Setup: Visual Studio Enterprise 2015, WPF, C#

  1. Go to Solution Explorer
  2. Right-click on your ProjectName then click 'Properties'.
  3. Click Application as shown below. Remember your icon name.

enter image description here

  1. Click 'Publish' on the left column.
  2. Click 'Options...' button on the right.
  3. 'Publish Options' window should pop up as shown below. Remember what's in the 'Product name:' field. In the example below, it's "MyProductName"

enter image description here

  1. Copy and paste the following code in your main class.


    private void SetAddRemoveProgramsIcon()
    {
        if (ApplicationDeployment.IsNetworkDeployed && ApplicationDeployment.CurrentDeployment.IsFirstRun)
        {
            try
            {
                var iconSourcePath = Path.Combine(System.Windows.Forms.Application.StartupPath, "MyIcon.ico");

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

                var myUninstallKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");
                if (myUninstallKey == null) return;

                var mySubKeyNames = myUninstallKey.GetSubKeyNames();
                foreach (var subkeyName in mySubKeyNames)
                {
                    var myKey = myUninstallKey.OpenSubKey(subkeyName, true);
                    var myValue = myKey.GetValue("DisplayName");
                    if (myValue != null && myValue.ToString() == "MyProductName") // same as in 'Product name:' field
                    {
                            myKey.SetValue("DisplayIcon", iconSourcePath);
                        break;
                    }
                }
            }
            catch (Exception uhoh)
            {
                //log exception
            }
        }
    }

  1. Call SetAddRemoveProgramsIcon in the constructor.


    public MainViewModel()
    {
        SetAddRemoveProgramsIcon();
    }

Upvotes: 1

ManjuVijayan
ManjuVijayan

Reputation: 348

The following code is what I used for solving the problem. I used Stack Overflow question Custom icon for ClickOnce application in 'Add or Remove Programs'.

    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, "hl772-2.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() == "admin")
                    {
                        myKey.SetValue("DisplayIcon", iconSourcePath);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
            }
        }
    }

Upvotes: 3

Related Questions