2009-01-27 25 views

Trả lời

25

Tạo một ServiceInstaller và thiết lập mô tả

private System.ServiceProcess.ServiceInstaller serviceInstaller = 
    new System.ServiceProcess.ServiceInstaller(); 
this.serviceInstaller.Description = "Handles Service Stuff"; 
+6

Chỉ cần thêm vào đó, bạn cũng có thể thiết lập serviceInstaller.DisplayName = "đẹp hơn tên hiển thị"; – CapBBeard

+0

exxelent. Tôi đã suy nghĩ một số mã hóa sẽ được requrired ah-la giải pháp này ... http://www.codeproject.com/KB/dotnet/dotnetscmdescription.aspx có lẽ điều này chỉ cần thiết cho VS2003? –

1

Ngoài ra bạn có thể, tạo ra một ServiceInstaller và trong cửa sổ thuộc tính của trình cài đặt dịch vụ bạn sẽ thấy một tài sản Mô tả bạn có thể thiết lập. Nếu bạn không muốn mã nó.

+1

Tôi khá chắc chắn rằng tôi đã thử đặt mọi thuộc tính mô tả trong trình cài đặt dịch vụ và dịch vụ và không có tiện ích nào trong số đó có vẻ hoạt động. Có lẽ tôi đã bỏ lỡ cái này. –

12

Để làm rõ về cách thực hiện điều này mà không cần sử dụng mã:

  • Thêm một trình cài đặt dịch vụ cho dự án của bạn như đã mô tả ở đây: http://msdn.microsoft.com/en-us/library/ddhy0byf%28v=vs.80%29.aspx

  • Mở cài đặt (vd ProjectInstaller.cs) trong thiết kế lượt xem.

  • Nhấp một lần vào thành phần trình cài đặt dịch vụ (ví dụ: serviceInstaller1) hoặc nhấp chuột phải vào nó và chọn Thuộc tính.

  • Trong ngăn Thuộc tính, đặt Mô tả và/hoặc Tên hiển thị (đây cũng là nơi bạn đặt StartType, v.v.) Mô tả có lẽ là tất cả những gì bạn muốn thay đổi, mặc dù bạn muốn hiển thị một tên hiển thị có thể đọc được nhiều hơn một chút (cột đầu tiên trong Trình quản lý dịch vụ), bạn cũng có thể làm như vậy.

  • Nếu muốn, hãy mở tệp nhà thiết kế được tạo tự động (ví dụ: ProjectInstaller.Designer.cs) để xác minh rằng thuộc tính đã được đặt chính xác.

  • Xây dựng giải pháp và cài đặt bằng cách sử dụng installutil.exe hoặc các phương tiện khác.

3

Sau khi tạo dự án trình cài đặt dịch vụ trong VS2010, bạn cần thêm ghi đè vào phương thức cài đặt trong lớp do VS tạo ra để tạo mục đăng ký cho mô tả dịch vụ của bạn.

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Configuration.Install; 
using System.ServiceProcess; 
using Microsoft.Win32; 

namespace SomeService 
{ 
    [RunInstaller(true)] 
    public partial class ProjectInstaller : System.Configuration.Install.Installer 
    { 
     public ProjectInstaller() 
     { 
      InitializeComponent(); 
     } 
     /// <summary> 
     /// Overriden to get more control over service installation. 
     /// </summary> 
     /// <param name="stateServer"></param>  
     public override void Install(IDictionary stateServer) 
     { 
      RegistryKey system; 

      //HKEY_LOCAL_MACHINE\Services\CurrentControlSet 
      RegistryKey currentControlSet; 

      //...\Services 
      RegistryKey services; 

      //...\<Service Name> 
      RegistryKey service; 

      // ...\Parameters - this is where you can put service-specific configuration 
      // Microsoft.Win32.RegistryKey config; 

      try 
      { 
       //Let the project installer do its job 
       base.Install(stateServer); 

       //Open the HKEY_LOCAL_MACHINE\SYSTEM key 
       system = Registry.LocalMachine.OpenSubKey("System"); 
       //Open CurrentControlSet 
       currentControlSet = system.OpenSubKey("CurrentControlSet"); 
       //Go to the services key 
       services = currentControlSet.OpenSubKey("Services"); 

       //Open the key for your service, and allow writing 
       service = services.OpenSubKey("MyService", true); 
       //Add your service's description as a REG_SZ value named "Description" 
       service.SetValue("Description", "A service that does so and so"); 
       //(Optional) Add some custom information your service will use... 
       // config = service.CreateSubKey("Parameters"); 
      } 
      catch (Exception e) 
      { 

       throw new Exception(e.Message + "\n" + e.StackTrace); 
      } 
     } 
    } 
} 

http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.aspx

http://www.codeproject.com/KB/dotnet/dotnetscmdescription.aspx

+0

Yup ... 'ServiceInstaller.Description' không làm gì, nhưng thêm nó bằng tay như thế này hoạt động tốt. – Nyerguds

+0

Huh. Đừng bận tâm. Mô tả đã hoạt động; đầu vào của tôi của nó bị lỗi trên một mức độ hoàn toàn khác. – Nyerguds

0

Bạn cũng có thể đặt tên dịch vụ và mô tả từ IDE, bằng cách click chuột phải vào biểu tượng "serviceInstaller" trong giao diện thiết kế của lớp ProjectInstaller.

Setting service Description from IDE

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