gpt4 book ai didi

c# - C# ClickOnce 应用程序的“添加或删除程序”图标

转载 作者:太空狗 更新时间:2023-10-29 21:34:29 24 4
gpt4 key购买 nike

我已经尝试了 Stack Overflow 问题中的解决方案 Custom icon for ClickOnce application in 'Add or Remove Programs' Is there a way to change the icon of a ClickOnce application in 'Add or Remove Programs'? .

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

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

最佳答案

在查看注册表并复制其他应用程序的设置后,我终于弄明白了。奇怪的是,您无法在 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());
}
}
}

关于c# - C# ClickOnce 应用程序的“添加或删除程序”图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16228550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com