9

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ManjuVijayan
  • 338
  • 1
  • 5
  • 19

2 Answers2

3

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());
            }
        }
    }
Community
  • 1
  • 1
ManjuVijayan
  • 338
  • 1
  • 5
  • 19
  • 1
    I tried this solution, but it doesn't change the image shown at the installation window (like in the screenshot of the opening post). Does someone have a solution for that as well? – Peter van Kekem Jan 19 '15 at 13:20
1

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();
    }

icernos
  • 395
  • 3
  • 6
  • System.Windows.Forms.Application.StartupPath did not work for me. I had to use System.IO.Directory.GetCurrentDirectory() for my ClickOnce Installation as path for the Icon in Add and Remove Programms – halliballi Feb 24 '23 at 13:03