monkshorin
monkshorin

Reputation: 21

Service writing files in a different path

I have 1 form and 1 service that should be writting to the same location. So I added the following line in both.

Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "myFolder" + Path.DirectorySeparatorChar + "log.txt");

the problem is that the form is writing the file in the right location but the service isn't.
The service is writing in C:\Windows\System32\config\systemprofile\AppData\Roaming\myFolder\log.txt and the form in C:\Users\<user>\AppData\Roaming\myFolder\log.txt.

I can't use the windows event logger and i got to handle some other files too.

Upvotes: 2

Views: 435

Answers (2)

zmbq
zmbq

Reputation: 39013

Put the folder name in a config file. Or at least put the user's name, so you can build the folder name properly in the service.

Upvotes: 0

Steven V
Steven V

Reputation: 16595

The user that runs the service is either NETWORK SERVICE or LOCAL SERVICE, instead of a real user account. Under those special accounts, Environment.SpecialFolder.ApplicationData comes to the systemprofile directory.

So to fix that, you'll need to change the user the service runs under, or hard code/determine the correct user directory to use.

Upvotes: 2

Related Questions