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?
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With