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

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

问题描述

尽管有很多关于.net配置文件的帖子,我相信我的要求不允许任何提出的解决方案(或者我不明白这个过程,以使其适用于我)。 p>

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



我想做的是允许用户导出和导入所有保存的设置。而不是使用一些自定义对象或使用INI文件进行序列化,我认为只需保存和替换应用程序使用的配置文件就会更容易。



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

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

但是,恢复配置文件以覆盖当前设置证明是困难的。 似乎我可以打开这样的配置文件

  var newConfig = ConfigurationManager.OpenExeConfiguration(settingsFilePath)

但是我看不到如何将当前配置中的所有设置全部替换为导入文件的设置。



也许我只需要使用其他帖子中概述的方法仅替换一部分配置,而不是整个事情,但是我看不出在这一点上如何工作。



任何想法?我要走下正确的轨道,还是应该使用INI文件(或其他东西)?

解决方案

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


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

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.

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. [Edit: This overload is supposed to receive the path of the .exe, not the .config file. Opening an exe file in this manner will possibly throw a ConfigurationErrorsException if the calling code doesn't reference the same assemblies in the config 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.

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天全站免登陆