views:

859

answers:

9

How can I find the location of exe that runs as a windows service in run-time?

A: 

If your executable attaches itself to a particular port you could parse the output from

netstat -ab

Probably not the most attractive solution though.

Harry Lime
+2  A: 

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.

MusiGenesis
+1  A: 

In .NET, you can use the ServiceController Class.

Galwegian
A: 

Usually they run under windows\system32 even though you may have installled it on another drive

+1  A: 

.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, ...)

P Daddy
+3  A: 

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.

PhiLho
+2  A: 

A. Use a registry look-up:

e.g.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\' + ServiceDisplayName;

then read 'ImagePath' value

tomsee
A: 

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.

Matt Dillard
A: 

For native windows code: GetModuleFileName(NULL...) in the EXE (not in a DLL loaded by the service, for example).

a_mole