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);
}
}
}
}
6条答案
按热度按时间liwlm1x91#
创建ServiceInstaller并设置描述
yrwegjxp2#
要阐明如何在不使用代码的情况下完成此操作:
installutil.exe
或其他方法生成解决方案并安装。yacmzcpb3#
在VS2010中创建服务安装程序项目之后,需要向VS创建的类中的Install方法添加一个重写,以便为服务描述创建注册表项。
http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.aspx
http://www.codeproject.com/KB/dotnet/dotnetscmdescription.aspx
4jb9z9bj4#
您还可以通过右键单击ProjectInstaller类的设计视图中的“serviceInstaller”图标,在IDE中设置服务名称和描述。
piok6c0g5#
你也可以创建一个服务安装程序,在服务安装程序的属性窗口中,你会看到一个描述属性,如果你不想编码它,你可以设置它。
ergxz8rk6#
除了列出的其他解决方案之外,请确保您使用installutil.exe而不是sc.exe安装服务。这两种方法都会将服务添加到您的服务控制面板,从那里可以启动和停止服务等。但installutil.exe是唯一一个可以设置“不错”功能(如Description和DisplayName)的方法。