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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/485268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:53:41  来源:igfitidea点击:

What's the best way to set a windows service description in .net

.netwindows

提问by Kevin Gale

I have created a C# service using the VS2005 template. It works fine however the description of the service is blank in the Windows Services control applet.

我已经使用 VS2005 模板创建了一个 C# 服务。它工作正常,但是在 Windows 服务控制小程序中该服务的描述是空白的。

回答by Nick

Create a ServiceInstaller and set the description

创建一个 ServiceInstaller 并设置描述

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

回答by Tobias J

To clarify on how to accomplish this without using code:

要阐明如何在不使用代码的情况下完成此操作:

  • Add a service installer to your project as described here: http://msdn.microsoft.com/en-us/library/ddhy0byf%28v=vs.80%29.aspx

  • Open the installer (e.g. ProjectInstaller.cs) in Design view.

  • Single-click the service installer component (e.g. serviceInstaller1) or right-click it and choose Properties.

  • In the Properties pane, set the Description and/or DisplayName (this is also where you set StartType etc.) Description is probably all you want to change, although if you want to give a slightly more human-readable DisplayName (the first column in Services manager) you can also do so.

  • If desired, open the auto-generated designer file (e.g. ProjectInstaller.Designer.cs) to verify that the properties were set correctly.

  • Build the solution and install using installutil.exeor other means.

  • 将服务安装程序添加到您的项目中,如下所述:http: //msdn.microsoft.com/en-us/library/ddhy0byf%28v=vs.80%29.aspx

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

  • 单击服务安装程序组件(例如 serviceInstaller1)或右键单击它并选择“属性”。

  • 在 Properties 窗格中,设置 Description 和/或 DisplayName(这也是您设置 StartType 等的位置)Description 可能就是您想要更改的全部内容,但如果您想提供一个更易读的 DisplayName(在服务经理)您也可以这样做。

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

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

回答by bizcad

After you create your service installer project in VS2010, you need to add an override to the Install method in the class created by VS to create the registry entry for your service description.

在 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://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.aspx

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

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

回答by Fiach Reid

You can also set the service name and description from the IDE, by right clicking on the "serviceInstaller" icon in the design view of ProjectInstaller class.

您还可以在 IDE 中设置服务名称和描述,方法是在 ProjectInstaller 类的设计视图中右键单击“serviceInstaller”图标。

Setting service Description from IDE

从 IDE 设置服务描述

回答by Fiach Reid

Also you could, create a ServiceInstaller and in the properties window of the Service installer you will see a Description Property you can set. If you don't want to code it.

您也可以创建一个 ServiceInstaller,然后在 Service 安装程序的属性窗口中,您将看到一个可以设置的描述属性。如果你不想编码。