如何使用.NET 配置文件(app.config、settings.settings)来保存和恢复所有应用程序数据? [英] How to use .NET configuration files (app.config, settings.settings) to save and restore all application data?

查看:35
本文介绍了如何使用.NET 配置文件(app.config、settings.settings)来保存和恢复所有应用程序数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管有很多关于 .net 配置文件的帖子,但我相信我的要求不允许提出的任何解决方案(或者我对流程的了解不足以使其对我有用).

Although there are a lot of posts about .net config files, I believe that my requirements do not allow for any of the solutions proposed (or I don't understand the process enough to make it work for me).

情况是我有一个 Windows 窗体应用程序(也可以是另一个应用程序),它将某些用户输入字段(例如 IP 地址)以及表单属性(窗口大小等)绑定到应用程序设置(在属性->设置区域).据我了解,这些设置与我项目的 app.config 文件中表示的设置相同,因此我应该能够使用 System.Configuration.ConfigurationManager 类来操作这些设置.

The situation is that I have a Windows Forms application (could just as well be another app) which binds certain user input fields (e.g. IP Address), as well as form properties (window size, etc) to application settings (in the Properties->Settings area). From what I understand, these settings are the same as those represented in my project's app.config file, so I should be able to use the System.Configuration.ConfigurationManager class to manipulate those settings.

我想要做的是允许用户导出和导入所有保存的设置.与其对某些自定义对象进行序列化或使用 INI 文件,我认为保存和替换应用程序使用的配置文件会更容易.

What I want to do is to allow the user to export and import all of the saved settings. Rather than doing serialization of some custom object or working with INI files, I thought it would be easier to just save and replace the config file that the application uses.

将当前设置保存到指定文件相对容易:

It is relatively easy to save the current settings to a specified file:

internal static void Export(string settingsFilePath)
{
    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
    config.SaveAs(settingsFilePath);
}

然而,恢复配置文件以覆盖当前设置被证明是困难的.好像可以这样打开配置文件

However, restoring a config file to overwrite the current settings is proving difficult. It seems like I can open a configuration file like so

var newConfig = ConfigurationManager.OpenExeConfiguration(settingsFilePath)

但我不知道如何将当前配置中的所有设置完全替换为导入文件的设置.

but I don't see how to completely replace all of the settings in the current config with those of the imported file.

也许我只需要使用其他帖子中概述的方法 仅替换配置的一部分,而不是整个配置,但目前我不知道这将如何工作.

Perhaps I only need to use the methods outlined in other posts to replace only a section of the config, and not the whole thing, but I don't see how that would work at this point.

有什么想法吗?我是在走正确的道路,还是应该只使用 INI 文件(或其他文件)?

Any ideas? Am I going down the right track, or should I just use INI files (or something else)?

推荐答案

由于我没有收到任何其他答案,并且对我之前的解决方案不满意,所以我又问了这个问题,又做了一些研究,终于能够来了提出更好的答案.请参阅如何加载单独的动态应用程序设置文件并与当前设置合并?代码和解释.

Since I did not receive any other answers and was not happy with my previous solution, I asked the question again, did some more research, and was able to come up with a better answer. See How to load a separate Application Settings file dynamically and merge with current settings? for the code and explanation.

这篇关于如何使用.NET 配置文件(app.config、settings.settings)来保存和恢复所有应用程序数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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