How to find windows service exe path

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 path ?


You can use AppDomain.CurrentDomain.BaseDirectory


Tip: If you want to find startup path of installed windows service, look here from registry .

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\ + ServiceName

There are keys about windows service


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());
                }