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

前端 未结 5 892
离开以前
离开以前 2021-02-18 16:12

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.

相关标签:
5条回答
  • 2021-02-18 16:45

    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.exe or other means.

    0 讨论(0)
  • 2021-02-18 16:47

    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.

    0 讨论(0)
  • 2021-02-18 16:48

    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.

    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

    0 讨论(0)
  • 2021-02-18 16:52

    Create a ServiceInstaller and set the description

    private System.ServiceProcess.ServiceInstaller serviceInstaller = 
      new System.ServiceProcess.ServiceInstaller();
    this.serviceInstaller.Description = "Handles Service Stuff";
    
    0 讨论(0)
  • 2021-02-18 16:54

    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.

    0 讨论(0)
提交回复
热议问题