我已经尝试了 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());
}
}
}
*/