简体   繁体   English

如何在用C#编写的Windows服务中获取ServiceName?

[英]How can I get the ServiceName in a Windows Service written in C#?

How can I get the service name programatically using this.ServiceName in a Windows Service written in C#? 如何在C#编写的Windows服务中使用this.ServiceName以编程方式获取服务名称? When I try this, it's asking for assembly reference but its not accepting anything: 当我尝试此操作时,它要求提供程序集引用,但不接受任何内容:

string path = this.ServiceName + ".config";

but its getting error. 但它得到错误。

Your service needs to inherit ServiceBase from System.ServiceProcess.dll . 您的服务需要从System.ServiceProcess.dll继承ServiceBase

When you will have that you will be able to access this.ServiceName property. 当拥有时,您将能够访问this.ServiceName属性。

Sample: 样品:

public partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        string test = this.ServiceName;
    }

    protected override void OnStop()
    {
    }
}

If you want to access it from Main() (Program class), then you can do something like this: 如果要从Main()(程序类)访问它,则可以执行以下操作:

namespace WindowsService1
{
    static class Program
    {
        static ServiceBase[] _servicesToRun;

        static void Main()
        {
            _servicesToRun = new ServiceBase[] 
            { 
                new Service1() 
            };

            string serviceName = _servicesToRun[0].ServiceName;

            ServiceBase.Run(_servicesToRun);
        }
    }
}

To find the .config file for your service, do not use the service name. 要为您的服务找到.config文件,请不要使用服务名称。 That's not how the CLR works, it selects the .config file based on the EXE name. 这不是CLR的工作方式,它会根据EXE名称选择.config文件。 Fix: 固定:

 var path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

To access the configuration file you need the application name not the service name (if you are using standard .Net application configuration files). 要访问配置文件,您需要应用程序名称而不是服务名称(如果使用的是标准.Net应用程序配置文件)。

To get the application executable use System.AppDomain.CurrentDomain.FriendlyName 要获取应用程序可执行文件,请使用System.AppDomain.CurrentDomain.FriendlyName

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows 服务如何确定其服务名称? - How can a Windows Service determine its ServiceName? 无法在计算机“。”上打开Servicename服务。 使用C#以编程方式启动Windows Service时出错 - Cannot open Servicename service on computer '.'. Error on Starting Windows Service programatically using c# 我如何在c#中等待一段时间(Windows服务) - How I can waiting some time in c# (windows service) 如何将DateTime对象传递给从PHP用C#编写的Soap Web服务? - How can I pass a DateTime object to a Soap web service written with C# from PHP? 如何在C#中的Windows服务中执行批处理脚本? - How can I execute a batch script in a windows service in C#? 如何用C#编写的类库引用用C ++编写的Windows运行时组件? - How can Windows Runtime Component written in C++ be referenced from Class Library written in C#? 是否为C#Windows服务提供OnRestart()事件侦听器? 如何查看Windows服务是否已重新启动? - Is there a OnRestart() event listener for a C# Windows service? How can I see if the Windows Service was Restarted? 如何获取 DataGridView combobox 单元格值以写入 C# 的 MessageBox 中? - How can I get DataGridView combobox cell value to be written in the MessageBox in C#? 如何在C#中创建可以与GUI *或*一起运行的Windows应用程序? - How can I create a Windows application that can run with a GUI *or* as a Windows service in C#? 如何在C#编写的Android中声明和设置Cursor - How can I declare and set Cursor in Android written by C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM