Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the install directory of a Windows Service, using C#?

I'm pretty sure that a Windows service gets C:\winnt (or similar) as its working directory when installed using InstallUtil.exe. Is there any way I can access, or otherwise capture (at install time), the directory from which the service was originally installed? At the moment I'm manually entering that into the app.exe.config file, but that's horribly manual and feels like a hack.

Is there a programmatic way, either at run time or install time, to determine where the service was installed from?

like image 437
endian Avatar asked Jan 29 '09 14:01

endian


People also ask

How do I find the path of a Windows service?

The physical path is 'C:\WINDOWS\TEMP\. net\FreshIQAppMessagingService\zu4jbgzc. let AppDomain.

Where is InstallUtil EXE located?

The InstallUtil binary may also be digitally signed by Microsoft and located in the . NET directories on a Windows system: C:\Windows\Microsoft.NET\Framework\v \InstallUtil.exe and C:\Windows\Microsoft.NET\Framework64\v \InstallUtil.exe .

What is the install directory?

The installation directory contains files such as executable files and default files that do not change when the system is running. You can change the default installation directory, but for a multiple server installation, use the same installation directory path on all servers.

What is installation directory path of your directory?

Generally on a 64 Bit Computer, 32 bit programs are stored in C:\Program Files (x86) However the 64 Bit programs on a 64 Bit computer will be stored in C:\Program Fileswhich is the same as the directory for all 32 Bit computers programs. jd3sp4o0y and 26 more users found this answer helpful. heart outlined.


2 Answers

You can use reflection to get the location of the executing assembly. Here's a simple routine that sets the working directory to the location of the executing assembly using reflection:

String path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = System.IO.Path.GetDirectoryName(path);
Directory.SetCurrentDirectory(path);
like image 187
Steve Wranovsky Avatar answered Oct 15 '22 10:10

Steve Wranovsky


Do you mean you want the directory containing the assembly? If so, that's easy: use Assembly.Location.

I wouldn't try to change the working directory of the process though - I wouldn't be surprised if that had nasty side effects, if indeed you're allowed to do it.

like image 40
Jon Skeet Avatar answered Oct 15 '22 11:10

Jon Skeet