问题描述
我的机器上运行了Windows服务。当我做一个 Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)
它给我空字符串,但是当我在控制台应用程序中做同样的事情时,它会给我MyDocuments文件夹的正确位置。
任何想法可能会发生什么?
您运行应用程序作为您正在运行的控制台,登录到计算机,您和您的用户,因此可以访问您的我的文档。当Windows服务运行时(默认情况下),它将以不具有我的文档文件夹的本地系统运行。这是因为Windows服务在计算机启动时运行,并且不需要用户登录。
问题是,您尝试使用My文件夹文件夹和Windows服务可能需要访问不依赖用户的位置?
最后,要想清楚,如果您在计算机上安装了10个用户。没有人登录时,Windows服务使用哪个用户的我的文档?
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?
这篇关于SpecialFolder.MyDocuments在控制台应用程序中提供正确的值,但不提供Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!