2013-04-26 25 views
5

Tôi đã thử giải pháp trong câu hỏi Stack Overflow Custom icon for ClickOnce application in 'Add or Remove Programs'Is there a way to change the icon of a ClickOnce application in 'Add or Remove Programs'?.'Thêm hoặc Xóa Chương trình' biểu tượng cho ứng dụng C# ClickOnce

Vì vậy, dưới đây là các triển khai của tôi. Cả hai người trong số họ biên dịch và không có ngoại lệ được ném khi mã được chạy. Tôi đang xuất bản các tệp thiết lập ClickOnce cho máy chủ web và sau đó cài đặt nó sau khi gỡ cài đặt từ máy tính. Khi tôi vào bảng điều khiển Thêm hoặc Xóa Chương trình, tôi vẫn thấy biểu tượng chung cho chương trình của mình. Ở khắp mọi nơi khác tôi không có vấn đề và biểu tượng của tôi chỉ hiển thị tốt.

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

Trả lời

3

Cuối cùng tôi đã tìm ra sau khi xem đăng ký và sao chép cài đặt của ứng dụng khác. Thật lạ khi bạn không thể tham chiếu tệp EXE trong ứng dụng được triển khai ClickOnce. Ít nhất tôi không thể làm cho nó hoạt động được. Vì vậy, tôi đã hoàn nguyên để tham khảo .ico thay thế. Hãy chắc chắn để đọc các ý kiến!

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

Tại sao chúng ta không thể thực hiện việc này mà không cần hack registry? – HackSlash

0

Bạn chỉ cần làm điều đó bằng mã sau.

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

Tôi theo cùng kỹ thuật sử dụng VB và VS2013E. Các bước:

  1. Nhấp chuột phải vào nút dự án trong Solution Explorer.
  2. Thêm Exisitng -> Logo.ico
  3. Quan sát rằng tệp được thêm vào cây dự án.
  4. Nhấp chuột phải vào mục này và chọn "Thuộc tính".
  5. "Sao chép vào thư mục đầu ra" chọn "Sao chép luôn".

Các bước đảm bảo rằng tệp Logo.ico được đóng gói trong quá trình triển khai. Các khối mã như sau:

Imports System.Deployment.Application.ApplicationDeployment 
Imports System.Reflection 
Imports System.IO 
Imports Microsoft.Win32 

Module ControlPanelIcon 
    ' Call this method as soon as possible 
    ' Writes entry to registry 
    Public Function SetAddRemoveProgramsIcon() As Boolean 
     Dim iName As String = "iconFile.ico" ' <---- set this (1) 
     Dim aName As String = "appName" '  <---- set this (2) 
     Try 
      Dim iconSourcePath As String = Path.Combine(System.Windows.Forms.Application.StartupPath, iName) 
      If Not IsNetworkDeployed Then Return False ' ClickOnce check 
      If Not CurrentDeployment.IsFirstRun Then Return False 
      If Not File.Exists(iconSourcePath) Then Return False 
      Dim myUninstallKey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall") 
      Dim mySubKeyNames As String() = myUninstallKey.GetSubKeyNames() 
      For i As Integer = 0 To mySubKeyNames.Length Step 1 
       Dim myKey As RegistryKey = myUninstallKey.OpenSubKey(mySubKeyNames(i), True) 
       Dim myValue As Object = myKey.GetValue("DisplayName") 
       If (myValue IsNot Nothing And myValue.ToString() = aName) Then 
        myKey.SetValue("DisplayIcon", iconSourcePath) 
        Return True 
       End If 
      Next i 
     Catch ex As Exception 
      Return False 
     End Try 
     Return False 
    End Function 
End Module 

Gọi hàm trả về true nếu hành động dự định được thực hiện. Sai khác. Trong Form chính, gọi hàm như thế này:

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 

Nhờ có sự đóng góp của việc đăng tải này, và đặc biệt nhờ Custom icon for ClickOnce application in 'Add or Remove Programs'.

Các vấn đề liên quan