[英]What's the best way to set a windows service description in .net
我使用VS2005模板創建了一個C#服務。 它工作正常,但Windows服務控件小程序中的服務描述為空。
創建ServiceInstaller並設置描述
private System.ServiceProcess.ServiceInstaller serviceInstaller =
new System.ServiceProcess.ServiceInstaller();
this.serviceInstaller.Description = "Handles Service Stuff";
為了澄清如何在不使用代碼的情況下完成此任務:
按照此處所述向項目添加服務安裝程序: http : //msdn.microsoft.com/en-us/library/ddhy0byf%28v=vs.80%29.aspx
在“設計”視圖中打開安裝程序(例如ProjectInstaller.cs)。
單擊服務安裝程序組件(例如serviceInstaller1)或右鍵單擊它,然后選擇“屬性”。
在“屬性”窗格中,設置“描述”和/或“顯示名稱”(這也是您設置StartType等的位置。)描述可能只是您想要更改的內容,但是如果您想要提供稍微更人性化的DisplayName(第一列中的服務經理)你也可以這樣做。
如果需要,打開自動生成的設計器文件(例如ProjectInstaller.Designer.cs)以驗證屬性是否已正確設置。
構建解決方案並使用installutil.exe
或其他方式進行安裝。
在VS2010中創建服務安裝程序項目后,需要在VS創建的類中為Install方法添加覆蓋,以便為服務描述創建注冊表項。
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
您也可以創建一個ServiceInstaller,在Service安裝程序的屬性窗口中,您將看到可以設置的Description屬性。 如果你不想編碼它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.