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

查看:276
本文介绍了如何使用.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地址),以及表单属性(窗口大小等),以应用程序设置(在属性 - >设置区域)。据我了解,这些设置都是一样重新psented在我的项目的app.config文件$ P $,所以我应该能够使用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)

但我没有看到如何完全取代当前的配置与导入文件的所有设置

也许我只需要使用<一个概述的方法href="http://stackoverflow.com/questions/132544/net-configuration-app-config-web-config-settings-settings">other帖子的更换配置的只有一个部分,而不是整个事情,但我看不出如何将工作在这一点上。

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

推荐答案

由于我没有收到任何其他的答案,并没有高兴我的previous的解决方案,我再次问这个问题,做了一些更多的研究,并且是能拿出一个更好的答案。请参阅<一href="http://stackoverflow.com/questions/2389290/how-to-load-a-separate-application-settings-file-dynamically-and-merge-with-curre">How加载一个独立的应用程序设置动态文件,并与当前设置合并?,在code和解释。

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