What directory does a Windows Service run in?

.NetWindows Services

.Net Problem Overview


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...?

.Net Solutions


Solution 1 - .Net

System.Diagnostics.Trace.WriteLine(Directory.GetCurrentDirectory());

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:

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

Solution 2 - .Net

Services are started from an application called Service Control Manager. This application lives in the system directory %WinDir%\System32

On a Windows 7 Ultimate - 64 bits this path is actually : %WinDir%\SysWOW64

For more information see Service Control Manager at MSDN.

Thanks Harper Shelby for pointing out problem with orginal post.

Solution 3 - .Net

You can make it work like so:

string cwd = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
Directory.SetCurrentDirectory(cwd ?? ".");

Solution 4 - .Net

Wanted also to know in which folder a Windows service was running but source code was not mine so could not modify it. Typing in Command Prompt sc qc <service name> displays the folder in BINARY_PATH_NAME.

C:\>sc qc
DESCRIPTION:
        Queries the configuration information for a service.
USAGE:
        sc <server> qc [service name] <bufferSize>

when query MyService get:

C:>sc qc MyService

[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: MyService
        TYPE               : 10  WIN32_OWN_PROCESS
        START_TYPE         : 3   DEMAND_START
        ERROR_CONTROL      : 1   NORMAL
        BINARY_PATH_NAME   : "D:\Routines\MyService\MyService.exe"
        LOAD_ORDER_GROUP   :
        TAG                : 0
        DISPLAY_NAME       : MyService
        DEPENDENCIES       :
        SERVICE_START_NAME : LocalSystem

Solution 5 - .Net

From https://stackoverflow.com/questions/29914673/sc-start-service-in-folder-start-in: One simple alternative is to use nssm.cc - this gives you the option of specifying a directory to start in.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGuyView Question on Stackoverflow
Solution 1 - .NetDirk VollmarView Answer on Stackoverflow
Solution 2 - .NetJeffView Answer on Stackoverflow
Solution 3 - .NetyantaqView Answer on Stackoverflow
Solution 4 - .NetRicardo stands with UkraineView Answer on Stackoverflow
Solution 5 - .NetSrinivasView Answer on Stackoverflow