我有一个使用 ClickOnce 技术部署的 Windows 应用程序。是否有办法更改应用程序在图像中显示的图标?
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();
}