问题
I use the following code to build a desired path for an XML file:
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
When executed from a Windows Forms application, it returns:
C:\Users\MyUsername\AppData\Roaming\...\Database.xml
When executed from within a Windows Service, it returns:
C:\Windows\system32\config\systemprofile\AppData\Roaming\...\Database.xml
I am now resorting to having a custom installer action persist the path in a registry key since the custom action will have a desktop context. If there is a better approach, please let me know.
What I am really interested in finding out is why the behavior is different in the first place. The Windows Service in question is set up to use the LocalSystem
account in case that matters.
回答1:
Only you know what the specific requirements are for this folder, but if it's to share data between multiple users, ApplicationData
is the wrong choice:
The directory that serves as a common repository for application-specific data for the current roaming user.
A roaming user works on more than one computer on a network. A roaming user's profile is kept on a server on the network and is loaded onto a system when the user logs on
See the SpecialFolder enumeration and pick an appropriate value that fits your requirements - one that doesn't depend on a specific user. Most of these start with Common
.
回答2:
I suggest that you should check and make sure that two apps are running by the same user. I tested your code in both winform app and webservice app, but their values are the same, so I can't find your real problem. good luck!
来源:https://stackoverflow.com/questions/20450158/consistent-applicationdata-path-between-winforms-apps-and-windows-service-contex