.net – What directory does a Windows Service run in


I've created a very simple .NET Windows Service and installed it using InstallUtil.exe utility.

In the service I have a piece of code as such:

if (File.Exists("test_file.txt"))
   // Do something clever

I've created a file called test_file.txt in the same directory as the service but the commented part of the code is never being executed…?

Best Solution


will output the current directory. Put that code in the startup method of your service and use a tool like DebugView to check the output. Then you will know the startup folder of your service.

This simple technique will be useful with many problems in service development, especially to debug service startup.

You probably expected the working folder of your service to be the folder where the service executable is in (so did I). You can change to that folder using the following lines of code: