6

我已经尝试了 Stack Overflow 问题中的解决方案,“添加或删除程序”中 ClickOnce 应用程序的自定义图标,以及有没有办法在“添加或删除程序”中更改 ClickOnce 应用程序的图标?.

所以,这是我的实现。它们都可以编译,并且在运行代码时不会引发异常。我将 ClickOnce 安装文件发布到网络服务器,然后在从计算机上卸载后安装它。当我转到控制面板Add or Remove Programs时,我仍然看到我的程序的通用图标。在其他任何地方我都没有问题,我的图标显示得很好。

/*  METHOD ONE */
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, "forico4.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());
        }
    }
}
*/

/*  METHOD TWO */
private static void SetAddRemoveProgramsIcon()
{
    //only run if deployed
    if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed
       && ApplicationDeployment.CurrentDeployment.IsFirstRun)
    {
        try
        {
            string iconSourcePath = Path.Combine(System.Windows.Forms.Application.StartupPath, "forico4.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() == "GoldMailAkamai")
                {
                    myKey.SetValue("DisplayIcon", iconSourcePath);
                    break;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}
*/
4

4 回答 4

5

在查看注册表并复制其他应用程序的设置后,我终于弄明白了。奇怪的是,您无法在 ClickOnce 部署的应用程序中引用 EXE 文件。至少我无法让它工作。所以,我.ico转而引用。请务必阅读评论!

using System.Deployment.Application;
using Microsoft.Win32;
//Call this method as soon as possible

private static void SetAddRemoveProgramsIcon()
{
    //Only execute on a first run after first install or after update
    if (ApplicationDeployment.CurrentDeployment.IsFirstRun)
    {
        try
        {
            // Set the name of the application EXE file - make sure to include `,0` at the end.
            // Does not work for ClickOnce applications as far as I could figure out... Note, this will probably work
            // when run from Visual Studio, but not when deployed.
            //string iconSourcePath = Path.Combine(System.Windows.Forms.Application.StartupPath, "example.exe,0");
            // Reverted to using this instead (note this will probably not work when run from Visual Studio, but will work on deploy.
            string iconSourcePath = Path.Combine(System.Windows.Forms.Application.StartupPath, "example.ico");
            if (!File.Exists(iconSourcePath))
            {
                MessageBox.Show("We could not find the application icon. Please notify your software vendor of this error.");
                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");
                Console.WriteLine(myValue.ToString());
                // Set this to the display name of the application. If you are not sure, browse to the registry directory and check.
                if (myValue != null && myValue.ToString() == "Example Application")
                {
                    myKey.SetValue("DisplayIcon", iconSourcePath);
                    break;
                }
            }
        }
        catch(Exception mye)
        {
            MessageBox.Show("We could not properly setup the application icons. Please notify your software vendor of this error.\r\n" + mye.ToString());
        }
    }
}
于 2013-05-01T15:52:26.940 回答
2

我使用 VB 和 VS2013E 遵循相同的技术。脚步:

  1. 右键单击解决方案资源管理器中的项目节点。
  2. 添加现有 -> Logo.ico
  3. 观察该文件已添加到项目树中。
  4. 右键单击该条目并选择“属性”。
  5. “复制到输出目录”选择“始终复制”。

这些步骤确保 Logo.ico 文件在部署中打包。代码块如下:

Imports System.Deployment.Application.ApplicationDeployment
Imports System.Reflection
Imports System.IO
Imports Microsoft.Win32

Module ControlPanelIcon
    ' Call this method as soon as possible
    ' Writes entry to registry
    Public Function SetAddRemoveProgramsIcon() As Boolean
        Dim iName As String = "iconFile.ico" ' <---- set this (1)
        Dim aName As String = "appName" '      <---- set this (2)
        Try
            Dim iconSourcePath As String = Path.Combine(System.Windows.Forms.Application.StartupPath, iName)
            If Not IsNetworkDeployed Then Return False ' ClickOnce check
            If Not CurrentDeployment.IsFirstRun Then Return False
            If Not File.Exists(iconSourcePath) Then Return False
            Dim myUninstallKey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall")
            Dim mySubKeyNames As String() = myUninstallKey.GetSubKeyNames()
            For i As Integer = 0 To mySubKeyNames.Length Step 1
                Dim myKey As RegistryKey = myUninstallKey.OpenSubKey(mySubKeyNames(i), True)
                Dim myValue As Object = myKey.GetValue("DisplayName")
                If (myValue IsNot Nothing And myValue.ToString() = aName) Then
                    myKey.SetValue("DisplayIcon", iconSourcePath)
                    Return True
                End If
            Next i
        Catch ex As Exception
            Return False
        End Try
        Return False
    End Function
End Module

如果执行了预期的操作,函数调用将返回 true。否则为假。在主窗体中,函数调用如下:

Sub New()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()
    ' Add any initialization after the InitializeComponent() call.
    ' Modify registry to show icon in Control Panel - Add/Remove Programs
    ControlPanelIcon.SetAddRemoveProgramsIcon()
End Sub

感谢此帖子的贡献者,特别感谢“添加或删除程序”中 ClickOnce 应用程序的自定义图标

于 2014-09-04T12:15:20.900 回答
1

如果您正在为 Outlook 等办公程序的 VSTO 加载项寻找添加/删除图标,请使用所选答案的以下修改版本。这将在部署后工作。

  private static void SetAddRemoveProgramsIcon()
        {


            //Only execute on a first run after first install or after update
            if (ApplicationDeployment.CurrentDeployment.IsFirstRun)
            {
                try
                {

                //Getting path to the installation directory
                System.Reflection.Assembly assemblyInfo = System.Reflection.Assembly.GetExecutingAssembly(); 
                string assemblyLocation = assemblyInfo.Location;
                Uri uriCodeBase = new Uri(assemblyInfo.CodeBase);
                string ClickOnceLocation = Path.GetDirectoryName(uriCodeBase.LocalPath.ToString());

                string iconSourcePath = Path.Combine(ClickOnceLocation, "youricon.ico");                
                    if (!File.Exists(iconSourcePath))
                    {
                        MessageBox.Show("We could not find the application icon. Please notify your software vendor of this error.");
                        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");                                            
                        // Set this to the display name of the application. If you are not sure, browse to the registry directory and check.
                        if (myValue != null && myValue.ToString() == "SafeShare Outlook Add-in")
                        {                        
                        myKey.SetValue("DisplayIcon", iconSourcePath);
                            break;
                        }
                    }
                }
                catch (System.Exception mye)
                {
                    MessageBox.Show("We could not properly setup the application icons. Please notify your software vendor of this error.\r\n" + mye.ToString());
                }
            }

             }
于 2019-12-19T13:00:57.893 回答
0

您可以通过以下代码简单地做到这一点。

string Install_Reg_Loc = @"Software\Microsoft\Windows\CurrentVersion\Uninstall";

string displayIcon = @"C:\MorganTech\setup-icon.ico";

RegistryKey hKey = (Registry.LocalMachine).OpenSubKey(Install_Reg_Loc, true);

RegistryKey appKey = hKey.OpenSubKey(productName);

appKey.SetValue("DisplayIcon", (object)displayicon, RegistryValueKind.String)
于 2013-09-06T04:27:03.217 回答