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

女生的网名这么多〃 提交于 2019-11-27 03:38:11

问题


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?


回答1:


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



回答2:


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.

  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"

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



来源:https://stackoverflow.com/questions/13245056/is-there-a-way-to-change-the-icon-of-a-clickonce-application-in-add-or-remove-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!