19

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?

5 Answers 5

35

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);
3
  • 1
    Doesn't this give C:\Windows\system32?
    – ProfK
    Commented Dec 7, 2014 at 7:33
  • @ProfK, no, it gives the location of the executing assembly. This would be the directory of the actual executable for the program being run. Commented Dec 16, 2014 at 17:18
  • My bad. I found my service looking for app.config in C:\Windows\system32 and assumed it thought its Location was there.
    – ProfK
    Commented Dec 16, 2014 at 19:16
7

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.

3
  • Thanks, Jon. You were right, the question was not worded quite correctly, so I've changed it.
    – endian
    Commented Jan 29, 2009 at 14:49
  • So Jon, has this been confirmed that one would NOT want to change the working folder of a service once it's install and/or running, or is there a way to get the working folder set during the Service INSTALL, so that you are able to access files/folders using relative paths, rather than absolute paths? I'm seeing the same thing with my service, which was written pretty much as an exe for testing/debugging, then defining a single symbol and compiling compiles it to a service, but it's always running out of %SYSTEMROOT%\System32...
    – LarryF
    Commented Feb 20, 2013 at 23:54
  • @LarryF: I don't know of any way to change the working directory used for a service, no. That doesn't mean there's no way of doing so, but I don't know of one.
    – Jon Skeet
    Commented Feb 21, 2013 at 6:44
5

I did not know the Directory.SetCurrentDirectory method. Usually I do:

Environment.CurrentDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
4

Though very late, but it may help somebody. I solved this issue by using AppDomain.CurrentDomain.BaseDirectory

string someFilePath = AppDomain.CurrentDomain.BaseDirectory + @"\Resources\SomeResource.xml";

AppDomain.CurrentDomain.BaseDirectory gave the directory where the windows service was actually isntalled, not the C:\Windows\system32\ path.

I saw it later that @Ramon has already posted the same solution.

0
2

InstallUtil.exe calls ServiceInstaller.Install() of your application at install time.

Override it, add it to the list of your project's Installers and get any information you need.

1
  • Thanks. The runtime approaches of Jon and Steve are actually a better fit for what I need to do.
    – endian
    Commented Jan 29, 2009 at 14:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.