Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpecialFolder.MyDocuments giving correct value in a console application but not on a Windows Service

I have a windows service running on my machine. When I do a

 Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)

it gives me empty string but when I do the same thing in a console application it gives me the correct location of my MyDocuments folder.

Any ideas as to why it might be happening?

like image 947
Kumar Vaibhav Avatar asked Dec 27 '22 01:12

Kumar Vaibhav


1 Answers

That is because when you run the application as a Console you are running signed in to the computer as you, the user, and can therefore access your "My Documents". When the Windows Service is running (by default) it will run as Local System which doesn't have a "My Documents" folder. This is because Windows Services run when the computer starts up and doesn't need a user signed in.

The question is, what are you trying to do with your "My Documents" folder and your Windows Service probably wants to access a location that is not user dependent?

Finally, to make it clear imagine if you had 10 users setup on the computer. Which user's "My Documents" would the Windows Service use when no-one has signed in yet?

like image 153
Belogix Avatar answered Jan 06 '23 14:01

Belogix