我有一个使用ClickOnce技术部署的Windows应用程序。有没有办法更改图像中显示的应用程序的图标?
答案 0 :(得分:2)
以下代码是我用来解决问题的代码。我使用了Stack Overflow问题 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());
}
}
}
答案 1 :(得分:0)
设置:Visual Studio Enterprise 2015,WPF,C#
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
}
}
}
public MainViewModel()
{
SetAddRemoveProgramsIcon();
}