WPF/C#:我应该在哪里保存用户首选项文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/396229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF/C#: Where should I be saving user preferences files?
提问by Sharun
What is the recommended location to save user preference files? Is there a recommended method for dealing with user preferences?
保存用户首选项文件的推荐位置是什么?是否有处理用户偏好的推荐方法?
Currently I use the path returned from typeof(MyLibrary).Assembly.Location
as a default location to store files generated or required by the application.
目前我使用返回的路径typeof(MyLibrary).Assembly.Location
作为默认位置来存储应用程序生成或需要的文件。
EDIT: I found two related/interesting questions:
编辑:我发现了两个相关/有趣的问题:
- Best place to save user information for Windows XP and Vista applications
- What's the way to implement Save / Load functionality?
EDIT #2:
This is just a note for people like me who had never used settings before.
Settings are pretty useful, but I had to do a whole bunch of digging to figure out what was going on (coming from the Python world, not something I am used too). Things got complicated as I wanted to save dictionaries and apparently they can't be serialized. Settings also seem to get stored in 3 different files depending on what you do. There is an app.config
, user.config
and a settings.setting
file. So here are two more links that I found useful:
编辑#2:这只是给像我这样以前从未使用过设置的人的注释。设置非常有用,但我必须进行大量挖掘才能弄清楚发生了什么(来自 Python 世界,我也没有使用过)。事情变得复杂了,因为我想保存字典,显然它们不能被序列化。根据您的操作,设置似乎也存储在 3 个不同的文件中。有一个app.config
,user.config
和一个settings.setting
文件。所以这里还有两个我觉得有用的链接:
采纳答案by Sailing Judo
You can use the Application Settings easily enough.
您可以很容易地使用应用程序设置。
If you haven't done so before just right click on the project and choose Properties. Select the Settings tab. Make sure you chose "User" for the scope (otherwise the setting is read-only).
如果您之前没有这样做,只需右键单击项目并选择属性。选择设置选项卡。确保为范围选择了“用户”(否则设置为只读)。
The code to access this is simple:
访问它的代码很简单:
forms.Width = Application1.Properties.Settings.Default.Width;
If you need to save it:
如果需要保存:
Application1.Properties.Settings.Default.Width = forms.Width;
Application1.Properties.Settings.Default.Save();
In the sample above, Width is the custom setting name you define in the Settings tab and Application1 is the Namespace of your application.
在上面的示例中,Width 是您在 Settings 选项卡中定义的自定义设置名称,Application1 是您的应用程序的命名空间。
Edit: Responding to further questions
编辑:回答进一步的问题
You mentioned you wanted to store Dictionary objects in the Settings. As you discovered, you can't do this directly because Dictionary objects are not serializable. However, you can create your own serializable dictionary pretty easily. Paul Welzer had an excellent example on his blog.
您提到要在“设置”中存储 Dictionary 对象。正如您所发现的,您不能直接执行此操作,因为 Dictionary 对象不可序列化。但是,您可以非常轻松地创建自己的可序列化字典。Paul Welzer在他的博客上有一个很好的例子。
You have a couple of links which sort of muddy the situation a little. Your original question is where to save "User Preference Files". I'm pretty certain Microsoft's intention with the Settings functionality is exactly that... storing user skin preferences, layout choices, etc. It not meant as a generic repository for an application's data although it could be easily abused that way.
你有几个链接,这让情况有点混乱。您最初的问题是在哪里保存“用户首选项文件”。我很确定微软对设置功能的意图正是...存储用户皮肤首选项、布局选择等。它并不意味着作为应用程序数据的通用存储库,尽管它很容易被滥用。
The data is stored in separate places for a good reason. Some of the settings are Application settings and are read-only. These are settings which the app needs to function but is not specific to a user (for example, URIs to app resources or maybe a tax rate). These are stored in the app.config.
数据存储在不同的地方是有充分理由的。一些设置是应用程序设置并且是只读的。这些是应用程序需要运行但不特定于用户的设置(例如,应用程序资源的 URI 或税率)。这些存储在 app.config 中。
User settings are stored in an obfuscated directory deep within the User Document/Settings folder. The defaults are stored in app.config (I think, can't recall for certain off the top of my head) but any user changes are stored in their personal folder. This is meant for data that changes from user to user. (By "user" I mean Windows user, not your app's user.)
用户设置存储在用户文档/设置文件夹深处的混淆目录中。默认值存储在 app.config 中(我想,我一时想不起来了),但任何用户更改都存储在他们的个人文件夹中。这适用于因用户而异的数据。(“用户”是指 Windows 用户,而不是您应用的用户。)
Hope this clarified this somewhat for you. The system is actually pretty simple. It might seem a little foreign at first but after a few days of using it you'll never have to think of it again... it just works.
希望这对您有所澄清。该系统实际上非常简单。一开始它可能看起来有点陌生,但是在使用它几天后,您将永远不必再想起它……它确实有效。
回答by Nir
When running as non-admin or on Vista you can't write to the "Program files" folder (or any sub folder of it).
当以非管理员身份运行或在 Vista 上运行时,您无法写入“程序文件”文件夹(或其任何子文件夹)。
The correct location to store user preference is (replace MyCompanyName and MyApplicationName with the correct names, obviously)
存储用户首选项的正确位置是(显然,用正确的名称替换 MyCompanyName 和 MyApplicationName)
On disk:
在磁盘上:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\MyCompanyName\MyApplicationName"
Or in the registry under the key:
或者在注册表项下:
HKEY_CURRENT_USER\Software\MyCompanyName\MyApplicationName
Those location are per-user and they work with non-admin user, several users using the same computer, fast user switching, terminal services and all the other ways people can interact with your software.
这些位置是每个用户,它们与非管理员用户、使用同一台计算机的多个用户、快速用户切换、终端服务以及人们可以与您的软件交互的所有其他方式一起工作。
If you need a common location for all users then:
如果您需要所有用户的公共位置,则:
- It will only work when the user run as an administrator
- It will not work reliably on Vista
- You have to take care of everything yourself (like two users running the application on the same computer at the same time via fast user switching).
- 仅当用户以管理员身份运行时才有效
- 它不能在 Vista 上可靠地工作
- 您必须自己处理所有事情(就像两个用户通过快速用户切换同时在同一台计算机上运行应用程序)。
and the locations are:
地点是:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationCommonData) + "\MyCompanyName\MyApplicationName"
Or in the registry under the key:
或者在注册表项下:
HKEY_LOCAL_MACHINE\Software\MyCompanyName\MyApplicationName
回答by Muad'Dib
You can use isolated storage. You can isolate by user, assembly and/or domain.
您可以使用隔离存储。您可以按用户、程序集和/或域进行隔离。
Introduction to Isolated Storage
http://msdn.microsoft.com/en-us/library/3ak841sy(VS.80).aspx
独立存储简介
http://msdn.microsoft.com/en-us/library/3ak841sy(VS.80).aspx
Types of Isolation
http://msdn.microsoft.com/en-us/library/eh5d60e1(VS.80).aspx
隔离类型
http://msdn.microsoft.com/en-us/library/eh5d60e1(VS.80).aspx
回答by Anne
the following seems to be the best option:
以下似乎是最好的选择:
Application.UserAppDataPath