Special Folder.MyDocuments giving the correct value in a console application but not on a Windows service

I have a Windows service running on my computer. When i do

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

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

Any ideas as to why this could be happening?

+4
source share
1 answer

This is due to the fact that when you start the application as a console, you log on to the computer as you, the user, and therefore can access your "My Documents". When the Windows service is running (by default), it will work as a local system that does not have a My Documents folder. This is because Windows services start when the computer starts and do not require user login.

The question is, what are you trying to do with the My Documents folder, and your Windows service probably wants to access a location that is independent of the user?

Finally, to make it clear, imagine if you had 10 users on your computer. Which My Documents user will use the Windows service if no one has signed up yet?

+8
source

Source: https://habr.com/ru/post/1489276/


All Articles