有没有办法在“添加或删除程序”中更改ClickOnce应用程序的图标?

时间:2012-11-06 05:40:35

标签: c# winforms icons clickonce

我有一个使用ClickOnce技术部署的Windows应用程序。有没有办法更改图像中显示的应用程序的图标?

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

2 个答案:

答案 0 :(得分:2)

以下代码是我用来解决问题的代码。我使用了Stack Overflow问题 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());
            }
        }
    }

答案 1 :(得分:0)

设置:Visual Studio Enterprise 2015,WPF,C#

  1. 转到解决方案资源管理器
  2. 右键单击您的ProjectName,然后单击“属性”。
  3. 单击“应用程序”,如下所示。记住您的图标名称。

enter image description here

  1. 单击左侧列上的“发布”。
  2. 单击右侧的“选项...”按钮。
  3. “发布选项”窗口应如下所示弹出。记住“产品名称:”字段中的内容。在下面的示例中,它是“ MyProductName”

enter image description here

  1. 将以下代码复制并粘贴到主类中。


    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. 在构造函数中调用SetAddRemoveProgramsIcon。


    public MainViewModel()
    {
        SetAddRemoveProgramsIcon();
    }