Windows service - get current directory

MoShe picture MoShe · Apr 29, 2012 · Viewed 61.7k times · Source

I have a Windows service that should look for a configuration file in its current directory.

So I use directory.getcurrentdirectiry() but instead of the service directory I get back

c:\windows\system32

Any idea why and how should I get the service directory?

Answer

Jed picture Jed · Apr 30, 2012

You can set the current directory to the directory that your service is running from by including this line in your code:

System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

The important part of this is:

System.AppDomain.CurrentDomain.BaseDirectory

That returns the path to the directory your service is running from.