Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';这是在.net中设置windows服务描述的最佳方法_.net_Windows - Fatal编程技术网

什么';这是在.net中设置windows服务描述的最佳方法

什么';这是在.net中设置windows服务描述的最佳方法,.net,windows,.net,Windows,我使用VS2005模板创建了一个C#服务。它工作正常,但是Windows服务控制小程序中的服务描述为空。创建服务安装程序并设置描述 private System.ServiceProcess.ServiceInstaller serviceInstaller = new System.ServiceProcess.ServiceInstaller(); this.serviceInstaller.Description = "Handles Service Stuff"; 您还可以创建一

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

创建服务安装程序并设置描述

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

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

要澄清如何在不使用代码的情况下完成此操作,请执行以下操作:

  • 将服务安装程序添加到项目中,如下所述:

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

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

  • 在“属性”窗格中,设置说明和/或显示名称(这也是您设置StartType等的地方)。说明可能是您想要更改的全部内容,但如果您想提供一个更易于阅读的显示名称(服务管理器中的第一列),您也可以这样做

  • 如果需要,请打开自动生成的设计器文件(例如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);
            }
        }
    }
 }
使用系统;
使用系统集合;
使用系统组件模型;
使用System.Configuration.Install;
使用System.ServiceProcess;
使用Microsoft.Win32;
命名空间服务
{
[运行安装程序(true)]
公共分部类ProjectInstaller:System.Configuration.Install.Installer
{
公共项目安装程序()
{
初始化组件();
}
/// 
///重写以获得对服务安装的更多控制。
/// 
///       
公共覆盖无效安装(IDictionary stateServer)
{
注册密钥系统;
//HKEY\U本地\U机器\Services\CurrentControlSet
注册表项currentControlSet;
//…\服务
注册密钥服务;
//...\
注册密钥服务;
//…\Parameters-这是您可以放置特定于服务的配置的位置
//Microsoft.Win32.RegistryKey配置;
尝试
{
//让项目安装程序完成它的工作
安装(stateServer);
//打开HKEY_LOCAL_MACHINE\系统密钥
system=Registry.LocalMachine.OpenSubKey(“系统”);
//开路电流控制集
currentControlSet=system.OpenSubKey(“currentControlSet”);
//转到服务键
services=currentControlSet.OpenSubKey(“服务”);
//打开服务的密钥,并允许写入
service=services.OpenSubKey(“MyService”,true);
//将您的服务描述添加为名为“description”的REG_SZ值
service.SetValue(“描述”,“一个这样做的服务”);
//(可选)添加服务将使用的一些自定义信息。。。
//config=service.CreateSubKey(“参数”);
}
捕获(例外e)
{
抛出新异常(e.Message+“\n”+e.StackTrace);
}
}
}
}


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


为了增加这一点,您还可以设置serviceInstaller.DisplayName=“更好的显示名称”;exxelent。我在想这个解决方案需要一些编码。。。也许这只是VS2003所必需的?我很确定我尝试过在服务和服务安装程序中设置每个description属性,但它们似乎都不起作用。也许我错过了这个。是的
ServiceInstaller.Description
没有执行任何操作,但是像这样手动添加它可以正常工作。没有关系。描述确实有效;我对它的输入在一个完全不同的层面上被窃听。