7

I have a bit weird situation. In the onstart method of a windows service if I put following code:

File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "\\TestFile.txt", "Started " + AppDomain.CurrentDomain.BaseDirectory);

it works and inside text file this is stored:

Started C:\Users\guser\Documents\Visual Studio 2012\Projects\FreeSpaceControlService\FreeSpaceControlService\bin\Debug\

The text file is created inside debug directory.

But if I use such code instead of the above(at the same place):

  File.AppendAllText("TestFile.txt", "Started");        

The text file is not created in the same directory as above. Why it is not created in the same directory? (It seems it was created in WIndows/System32 now that I check it - are there two executables? Why did the first return Debug directory?)

and where you typically(in which path) store settings for windows service and logs?

  • The environmental variables in the window service is not the same as when you are logged in. The owner of the service must be changed to run as a user instead of an admin. – jdweng Jun 04 '15 at 09:17

3 Answers3

4

Because the working directory of Windows services is %WinDir%\System32.

You can verify this for yourself:

File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "\\TestFile.txt",
                   "My working directory is: " + Directory.GetCurrentDirectory());

and where you typically(in which path) store settings for windows service and logs?

  • Settings: In the Windows registry.
  • Logs: In the Windows event log.
  • Any other files your service might need: Somewhere below System.Environment.SpecialFolder.CommonApplicationData (e.g. C:\ProgramData\myService). For more details, see the following question:
    What is the significance of the ProgramData folder in Windows?.
Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
2

I realize that this is an old question, but I wanted to provide an answer in the case that it could be useful for someone else that stumbles upon this question and needs an answer.

I needed to figure out something similar, i.e. calling an executable from the directory that the service is installed in, the answers to this question were helpful:

What directory does a Windows Service run in?

Ultimately, it was not the accepted answer for that question that was useful, but a different answer. I used this code to access the directory that the service was installed in:

Path.GetDirectoryName(Environment.GetCommandLineArgs()[0])
1

A relative path is based on the 'Current directory', AppDomain.CurrentDomain.BaseDirectory and Environment.CurrentDirectory are not necessarily the same.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    slight explanations what each other mean and how relate to windows services would be more appreciated –  Jun 04 '15 at 10:26
  • Yes, but it [all has been done before](http://www.c-sharpcorner.com/UploadFile/370e35/basedirectory-vs-currentdirectory-in-C-Sharp/) – H H Jun 04 '15 at 12:41