Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 383
Guy Avatar asked May 19 '09 20:05

Guy


People also ask

What folder are Windows services in?

By default, the current directory for your Windows service is the System32 folder.

How do I change the path of a Windows service?

To change the executable path of ServiceDesk go to below-mentioned location from registry. run -> regedit -> navigate to the below-mentioned location and highlight the ServiceDesk and from the right-hand side edit the ImagePath as required.

How do I see what Windows services are installed?

Windows has always used the Services panel as a way to manage the services that are running on your computer. You can easily get there at any point by simply hitting WIN + R on your keyboard to open the Run dialog, and typing in services. msc.


2 Answers

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); 
like image 157
Dirk Vollmar Avatar answered Sep 18 '22 12:09

Dirk Vollmar


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.

like image 40
Jeff Avatar answered Sep 19 '22 12:09

Jeff