C#中WinForms应用程序和Windows服务上下文之间的一致ApplicationData路径

时间:2021-06-06 23:26:25

I use the following code to build a desired path for an XML file:

我使用以下代码为XML文件构建所需的路径:

System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);

When executed from a Windows Forms application, it returns:

从Windows窗体应用程序执行时,它返回:

C:\Users\MyUsername\AppData\Roaming\...\Database.xml

When executed from within a Windows Service, it returns:

从Windows服务中执行时,它返回:

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.

我真正感兴趣的是,首先是行为不同的原因。有问题的Windows服务设置为在重要的情况下使用LocalSystem帐户。

2 个解决方案

#1


2  

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:

只有您知道该文件夹的具体要求是什么,但如果要在多个用户之间共享数据,则ApplicationData是错误的选择:

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.

请参阅SpecialFolder枚举并选择适合您要求的适当值 - 不依赖于特定用户的值。其中大多数都以Common开头。

#2


1  

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!

我建议您检查并确保两个应用程序由同一个用户运行。我在winform app和webservice app中测试了你的代码,但它们的值是相同的,所以我找不到你真正的问题。祝你好运!

#1


2  

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:

只有您知道该文件夹的具体要求是什么,但如果要在多个用户之间共享数据,则ApplicationData是错误的选择:

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.

请参阅SpecialFolder枚举并选择适合您要求的适当值 - 不依赖于特定用户的值。其中大多数都以Common开头。

#2


1  

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!

我建议您检查并确保两个应用程序由同一个用户运行。我在winform app和webservice app中测试了你的代码,但它们的值是相同的,所以我找不到你真正的问题。祝你好运!