问题
I have one Windows application which is deployed using ClickOnce technology. Is there a way to change the icon of that application which is shown in the image?

回答1:
The following code is what I used for solving the problem. I used Stack Overflow question 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());
}
}
}
回答2:
Setup: Visual Studio Enterprise 2015, WPF, C#
- Go to Solution Explorer
- Right-click on your ProjectName then click 'Properties'.
- Click Application as shown below. Remember your icon name.
- Click 'Publish' on the left column.
- Click 'Options...' button on the right.
- 'Publish Options' window should pop up as shown below. Remember what's in the 'Product name:' field. In the example below, it's "MyProductName"
- Copy and paste the following code in your main class.
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
}
}
}
- Call SetAddRemoveProgramsIcon in the constructor.
public MainViewModel()
{
SetAddRemoveProgramsIcon();
}
来源:https://stackoverflow.com/questions/13245056/is-there-a-way-to-change-the-icon-of-a-clickonce-application-in-add-or-remove-p