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

9

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

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


2
https://dev59.com/cWgu5IYBdhLWcg3w_b9V - Karthik
很好,发表你的解决方案而不是贴链接。:) - Karthik
2个回答

3
以下是我用于解决问题的代码。我使用了 Stack Overflow 上的一个问题 “在‘添加或删除程序’中为 ClickOnce 应用程序设置自定义图标” 的答案。
    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
我尝试了这个解决方案,但它并没有改变安装窗口中显示的图像(就像开头帖子中的截图一样)。有人也有解决办法吗? - Peter van Kekem

1

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

  1. 进入“解决方案资源管理器”。
  2. 右键点击项目名称,然后点击“属性”。
  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
            }
        }
    }


在构造函数中调用SetAddRemoveProgramsIcon。


    public MainViewModel()
    {
        SetAddRemoveProgramsIcon();
    }


System.Windows.Forms.Application.StartupPath 对我不起作用。 我不得不使用 System.IO.Directory.GetCurrentDirectory() 作为我的 ClickOnce 安装路径,以便在“添加和删除程序”中设置图标。 - halliballi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接