问题
How can I find the location of exe that runs as a windows service in run-time?
回答1:
.NET - Assembly.GetExecutingAssembly().Location
(others have suggested Application.ExecutablePath
, but this requires a reference to System.Windows.Forms
, which a service normally doesn't need)
Native - GetModuleFileName(NULL, ...)
回答2:
Use a registry look-up:
e.g.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\' + ServiceDisplayName;
then read ImagePath
value.
回答3:
Programmatically or with a tool?
In the latter case, I recommend using Sysinternals' Process Explorer: it shows all running processes, including services, and one of the fields is the command line used to run the process, including full path.
Their command line utility, PsService, can be useful too.
回答4:
If this is .NET, you want Application.ExecutablePath (if you're trying to get the running windows service's own application path). If you're trying to get the path of some other running windows service, that's a different story.
回答5:
If your executable attaches itself to a particular port you could parse the output from
netstat -ab
Probably not the most attractive solution though.
回答6:
If you're not using .NET, the most straight-forward way is to use Win32's ::QueryServiceConfig() function. This will give you the path name, the display name, and all sorts of other information about the service.
回答7:
For native windows code: GetModuleFileName(NULL...) in the EXE (not in a DLL loaded by the service, for example).
回答8:
Usually they run under windows\system32 even though you may have installled it on another drive
来源:https://stackoverflow.com/questions/298669/how-can-i-find-the-location-of-exe-that-runs-as-a-windows-service-in-run-time