WPF / C#:我应该在哪里保存用户首选项文件? [英] WPF/C#: Where should I be saving user preferences files?

查看:622
本文介绍了WPF / C#:我应该在哪里保存用户首选项文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保存用户首选项文件的建议位置是什么?有没有推荐的方法来处理用户偏好?



目前我使用从 typeof(MyLibrary).Assembly.Location 作为默认位置来存储应用程序生成或要求的文件。



编辑:
我发现两个相关/有趣的问题:





编辑#2:
这只是一个从来没有使用过设置的人的笔记。
设置是非常有用的,但我不得不做一大堆挖掘,以确定发生了什么(来自Python世界,而不是我使用的东西)。事情变得复杂,因为我想保存字典,显然它们不能被序列化。根据您的操作,设置也似乎存储在3个不同的文件中。有一个 app.config user.config 和一个 settings.setting 文件。所以这里有两个我发现有用的链接:




解决方案

您可以轻松使用应用程序设置。



如果您没有这样做,只需右键单击该项目并选择属性。选择设置选项卡。确保您为范围选择用户(否则设置为只读)。



访问此代码的代码很简单:

  forms.Width = Application1.Properties.Settings.Default.Width; 

如果您需要保存:

  Application1.Properties.Settings.Default.Width = forms.Width; 
Application1.Properties.Settings.Default.Save();

在上面的示例中,Width是您在设置选项卡中定义的自定义设置名称,Application1是您的应用程序的命名空间。



编辑:回应更多问题



您想在设置中存储字典对象。如您所发现的,您不能直接执行此操作,因为Dictionary对象不可序列化。但是,您可以很容易地创建自己的可序列化字典。 Paul Welzer在他的博客上有一个很好的例子 。 / p>

你有几个链接,一些泥泞的情况有点。您的原始问题是保存用户首选项文件的位置。我很确定Microsoft的设置功能正是这样的:存储用户皮肤偏好,布局选择等。它不是作为应用程序数据的通用存储库,尽管它可能容易被滥用。



数据存储在不同的地方,是一个很好的理由。一些设置是应用程序设置,只读。这些是应用程序需要运行但不是特定于用户的设置(例如,应用程序资源的URI或可能的税率)。这些存储在app.config中。



用户设置存储在用户文档/设置文件夹内的一个混淆的目录中。默认值存储在app.config中(我认为,我的头顶不能回忆起来),但任何用户更改都存储在他们的个人文件夹中。这是用于从用户更改为用户的数据。 (用户我的意思是Windows用户,而不是您的应用程序的用户。)



希望对您有所帮助。该系统实际上很简单。起初可能看起来有点外国人,但是在使用几天之后,你永远不会再想到它了...它只是起作用。


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.

EDIT: I found two related/interesting questions:

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:

解决方案

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.

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.

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.

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.)

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.

这篇关于WPF / C#:我应该在哪里保存用户首选项文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆