2013-04-26 5 views
5

ich die Lösung in Stack Overflow Fragen versucht haben Custom icon for ClickOnce application in 'Add or Remove Programs' und Is there a way to change the icon of a ClickOnce application in 'Add or Remove Programs'?.‚Programme hinzufügen oder entfernen‘ Symbol für eine C# Clickonce-Anwendung

Also, hier sind meine Implementierungen. Beide kompilieren und es werden keine Ausnahmen ausgelöst, wenn der Code ausgeführt wird. Ich veröffentliche die ClickOnce-Setup-Dateien auf einem Webserver und installiere sie dann nach der Deinstallation vom Computer. Wenn ich zur Systemsteuerung gehe Hinzufügen oder Entfernen von Programmen, sehe ich immer noch das generische Symbol für mein Programm. Überall sonst habe ich keine Probleme und mein Icon zeigt sich gut.

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

Antwort

3

Ich endlich herausgefunden, nachdem Sie die Registrierung und kopieren andere Einstellungen der Anwendung. Es ist seltsam, dass Sie die EXE-Datei in einer bereitgestellten ClickOnce-Anwendung nicht referenzieren können. Zumindest konnte ich es nicht zur Arbeit bringen. Also habe ich stattdessen auf die .ico verwiesen. Lesen Sie unbedingt die Kommentare!

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()); 
     } 
    } 
} 
+0

Warum können wir das nicht tun, ohne die Registrierung zu hacken? – HackSlash

0

Sie können es einfach durch den folgenden Code tun.

string Install_Reg_Loc = @"Software\Microsoft\Windows\CurrentVersion\Uninstall"; 

string displayIcon = @"C:\MorganTech\setup-icon.ico"; 

RegistryKey hKey = (Registry.LocalMachine).OpenSubKey(Install_Reg_Loc, true); 

RegistryKey appKey = hKey.OpenSubKey(productName); 

appKey.SetValue("DisplayIcon", (object)displayicon, RegistryValueKind.String) 
1

Ich folgte die gleiche Technik mit VB und VS2013E. Schritte:

  1. Klicken Sie mit der rechten Maustaste auf den Projektknoten im Projektmappen-Explorer.
  2. Hinzufügen Exisitng -> Logo.ico
  3. Beachten Sie, dass die Datei der Projektnavigation hinzugefügt wird.
  4. Klicken Sie mit der rechten Maustaste auf diesen Eintrag und wählen Sie "Eigenschaften".
  5. "In Ausgabeverzeichnis kopieren" Wählen Sie "Immer kopieren".

Die Schritte sichergestellt, dass die Logo.ico-Datei in der Bereitstellung verpackt ist. Folgende Codebausteine ​​stehen zur Verfügung:

Der Funktionsaufruf gibt "true" zurück, wenn die beabsichtigte Aktion ausgeführt wird. Falsch sonst. Im Haupt Form, Funktion Aufruf wie folgt:

Sub New() 
    ' This call is required by the Windows Form Designer. 
    InitializeComponent() 
    ' Add any initialization after the InitializeComponent() call. 
    ' Modify registry to show icon in Control Panel - Add/Remove Programs 
    ControlPanelIcon.SetAddRemoveProgramsIcon() 
End Sub 

Dank der Mitwirkenden dieses Posting, und besonderen Dank an Custom icon for ClickOnce application in 'Add or Remove Programs'.

Verwandte Themen