Reputation: 6968
How can I find the location of exe that runs as a windows service in run-time?
Upvotes: 2
Views: 7953
Reputation: 101
Use a registry look-up:
e.g.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\' + ServiceDisplayName;
then read ImagePath
value.
Upvotes: 10
Reputation: 525
For native windows code: GetModuleFileName(NULL...) in the EXE (not in a DLL loaded by the service, for example).
Upvotes: 0
Reputation: 14863
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.
Upvotes: 0
Reputation: 41162
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.
Upvotes: 4
Reputation: 29547
.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, ...)
Upvotes: 2
Reputation: 675
Usually they run under windows\system32 even though you may have installled it on another drive
Upvotes: -3
Reputation: 75396
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.
Upvotes: 1
Reputation: 29576
If your executable attaches itself to a particular port you could parse the output from
netstat -ab
Probably not the most attractive solution though.
Upvotes: 0