C# applicationSettings:如何更新 app.config? [英] C# applicationSettings: how to update app.config?

查看:63
本文介绍了C# applicationSettings:如何更新 app.config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 .NET 4.0,我想使用 app.config 文件来存储相同的参数设置.我执行以下操作.我使用项目属性中的 Settings 选项卡来创建我的参数.

I am using .NET 4.0 and I would like to use the app.config file to store same parameter settings. I do the following. I use the Settings tab in the project properties to create my parameters.

这在app.config文件中添加信息如下:

This add the information in the app.config file like this:

<MyApp.Properties.Settings>
      <setting name="param1" serializeAs="String">
        <value>True</value>
      </setting>
<MyApp.Properties.Settings>

在我的视图模型中(在我的代码中),我可以通过这种方式访问​​信息:

In my view model (in my code) I can access to the information in this way:

bool myBool = MyApp.Properties.Default.param1;

当我尝试更改配置文件中的值时,我尝试这样做:

When I try to change the value in the config file, I try this:

Properties.Settings.Default.param1 = false;

但这会导致一个错误,即 param1 是只读的.

But this causes an error, that param1 is read-only.

那么如何从我的代码更新我的配置文件?

So how can I update my config file from my code?

推荐答案

嗯,我看了 Hari Gillala 的链接,其中一位用户建议直接编辑 app.config 文件,即 xml 文件.

Well, I read the link of Hari Gillala, in which one user suggested to edit directly the app.config file, that is a xml file.

所以在项目属性-->设置中我创建了我需要的参数.然后,要在代码中加载参数,我执行以下操作:

So in project properties-->settings I create the parameters that I need. Then, to load a parameter in code I do the following:

_myViewModelProperty = MyApp.Properties.Settings.Default.MyParam1;

这样我就可以很容易的读取到config参数的信息了.是打字的,所以在设计的时候我可以看看分配是否正确.

In this way, I can read easily the information of the config parameter. Is typed, so in disign time I can see if the asign is correct or not.

为了更新配置文件,我使用 .NET 的 xml 库编辑 app.config 文件.

To update de config file, I edit the app.config file with the xml libraries of .NET.

    System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
    xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    System.Xml.XmlNode node;
    node = xml.SelectSingleNode("configuration/applicationSettings/MyApp.Properties.Settings/setting[@name='myparam1']");
    node.ChildNodes[0].InnerText = myNewValue;
    xml.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

这样我就创建了一个xml文档(xml变量),并加载了app.config文件的信息.然后,我搜索要更新的节点,更新其信息(InnerText 属性),最后保存更改.

In this way, I create a xml document (xml variable) and load the information of the app.config file. Then, I search for the node that I want to update, update its information (InnerText property) and finally I save the changes.

这样我就可以更新app.config了.这正是我想要的,因为在便携式应用程序中,只有一个用户会使用它,而且我希望该配置适用于我运行该应用程序的任何计算机.

In this way, I can update the app.config. It is what I want, because in a portable application, only one user will use it, and I want that the configuration is applied in any computer in which I run the application.

这篇关于C# applicationSettings:如何更新 app.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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