本文介绍了您如何在services.msc中使用具有不同名称的窗口服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用在services.msc中安装的具有不同名称的窗口服务.msc

Use of a window service with different names installed in services.msc

推荐答案

private void InitializeComponent()
{
    components = new System.ComponentModel.Container();

    processInstaller = new ServiceProcessInstaller();
    serviceInstaller = new ServiceInstaller();
    // Service will run under system account
    processInstaller.Account = ServiceAccount.LocalSystem;
    this.processInstaller.Password = null;
    this.processInstaller.Username = null;


    // Service will have Start Type of Manual
    serviceInstaller.StartType = ServiceStartMode.Automatic;

    string serviceDisplayName = GetConfigurationValue("ServiceDisplayName");
    string serviceName = GetConfigurationValue("ServiceName");

    if (string.IsNullOrEmpty(serviceDisplayName))
    {
        serviceDisplayName = "XXXXXXXXXXXXXXXXServiceName1";
    }

    if (string.IsNullOrEmpty(serviceName))
    {
        serviceName = "ServiceName1";
    }

    this.serviceInstaller.DisplayName = serviceDisplayName;
    this.serviceInstaller.ServiceName = serviceName;
    this.serviceInstaller.Description = "Description Service";
    //
    // ProjectInstaller
    //
    Installers.Add(serviceInstaller);
    Installers.Add(processInstaller);

}


private string GetConfigurationValue(string key)
{
    Assembly service = Assembly.GetExecutingAssembly(); // Assembly.GetAssembly(typeof(<<>DLL Name Here >));
    Configuration config = ConfigurationManager.OpenExeConfiguration(service.Location);

    if (config.AppSettings.Settings[key] != null)
    {
        return config.AppSettings.Settings[key].Value;
    } ;) 
    else
    {
        throw new IndexOutOfRangeException
            ("Settings collection does not contain the requested key: " + key);
    }
}


这篇关于您如何在services.msc中使用具有不同名称的窗口服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 14:21