【发布时间】:2012-11-06 05:40:35
【问题描述】:
我有一个使用ClickOnce 技术部署的Windows 应用程序。有没有办法更改图像中显示的该应用程序的图标?
【问题讨论】:
-
非常感谢.. 它对我有用。
-
太棒了..发布对您有用的东西,而不是发布链接..:)
标签: c# winforms icons clickonce
我有一个使用ClickOnce 技术部署的Windows 应用程序。有没有办法更改图像中显示的该应用程序的图标?
【问题讨论】:
标签: c# winforms icons clickonce
以下代码是我用来解决问题的代码。我使用了 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());
}
}
}
【讨论】:
设置: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();
}
【讨论】: