How to find windows service exe path

前端 未结 9 2061
一整个雨季
一整个雨季 2020-12-25 09:46

I have a windows service and I need to create directory to store some info. The directory path must be relative to the windows service exe file. How can get this exe file pa

9条回答
  •  无人及你
    2020-12-25 09:58

    To get path for service you can use Management object. ref: https://msdn.microsoft.com/en-us/library/system.management.managementobject(v=vs.110).aspx http://dotnetstep.blogspot.com/2009/06/get-windowservice-executable-path-in.html

    using System.Management;
    string ServiceName = "YourServiceName";
    using (ManagementObject wmiService = new ManagementObject("Win32_Service.Name='"+ ServiceName +"'"))
                    {
                        wmiService.Get();
                        string currentserviceExePath = wmiService["PathName"].ToString();
                        Console.WriteLine(wmiService["PathName"].ToString());
                    }
    

提交回复
热议问题