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

问题描述 投票:0回答:6

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

.net windows
6个回答
27
投票

创建一个

ServiceInstaller
并设置描述:

private System.ServiceProcess.ServiceInstaller serviceInstaller = 
    new System.ServiceProcess.ServiceInstaller();

this.serviceInstaller.Description = "Handles Service Stuff";

15
投票

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

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

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

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

  • 在“属性”窗格中,设置“描述”和/或“显示名称”(这也是您设置“开始类型”等的地方)。“描述”可能就是您想要更改的所有内容,但如果您想提供一个更易于理解的显示名称(第一个服务管理器中的列)您也可以这样做。

  • 如果需要,打开自动生成的设计器文件(例如 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);
            }
        }
    }
 }

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

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


3
投票

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

Setting service Description from IDE


1
投票

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


0
投票

除了列出的其他解决方案之外,请确保使用 installutil.exe 而不是 sc.exe 安装服务。两者都会将服务添加到您的服务控制面板,从那里可以启动和停止等。但是 installutil.exe 是唯一一个可以设置“好”功能(如描述和显示名称)的服务。

© www.soinside.com 2019 - 2024. All rights reserved.