【发布时间】:2009-01-27 21:12:23
【问题描述】:
我使用 VS2005 模板创建了一个 C# 服务。它工作正常,但是在 Windows 服务控制小程序中服务的描述是空白的。
【问题讨论】:
我使用 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.Description 没有做任何事情,但是像这样手动添加它可以正常工作。
您也可以创建一个 ServiceInstaller,然后在服务安装程序的属性窗口中,您将看到可以设置的描述属性。如果你不想编码。
【讨论】: