在.net中设置Windows服务描述的最佳方法是什么

Kev*_*ale 21 .net windows

我使用VS2005模板创建了一个C#服务.它工作正常,但Windows服务控件小程序中的服务描述为空.

Nic*_*ick 26

创建ServiceInstaller并设置描述

private System.ServiceProcess.ServiceInstaller serviceInstaller = 
  new System.ServiceProcess.ServiceInstaller();
this.serviceInstaller.Description = "Handles Service Stuff";
Run Code Online (Sandbox Code Playgroud)

  • 只需添加到此,您还可以设置serviceInstaller.DisplayName ="Nicer Display Name"; (6认同)

Tob*_*s J 14

为了澄清如何在不使用代码的情况下完成此任务:

  • 按照此处所述向项目添加服务安装程序:http://msdn.microsoft.com/en-us/library/ddhy0byf%28v=vs.80%29.aspx

  • 在"设计"视图中打开安装程序(例如ProjectInstaller.cs).

  • 单击服务安装程序组件(例如serviceInstaller1)或右键单击它,然后选择"属性".

  • 在"属性"窗格中,设置"描述"和/或"显示名称"(这也是您设置StartType等的位置.)描述可能只是您要更改的内容,但是如果您想要提供稍微更人性化的DisplayName(第一列中的服务经理)你也可以这样做.

  • 如果需要,打开自动生成的设计器文件(例如ProjectInstaller.Designer.cs)以验证属性是否已正确设置.

  • 构建解决方案并使用installutil.exe或其他方式安装.


小智 6

在 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);
            }
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

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

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