如何从另一个更新一个app.config文件的内容 [英] How to update the contents of one app.config file from another

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

问题描述

亲爱的编码员,

我想从共享位置中的其他配置文件更新我的app.config文件的内容.请帮我编码的一部分.

shared.config文件

Dear Coders,

I want to update the contents of my app.config file from other config file which is in a shared location. Please help me the coding part of it.

shared.config file

!appsettings!
!add key="loc1" value="location1"!
!/appsettings!


app.config文件


app.config file

!appsettings!
!add key="loc" value="location"!
!/appsettings!



现在,我如何才能通过单击按钮来反映app.config文件中存在的位置更改与shared.config文件中存在的位置更改.在此先感谢您.



now how can i reflect the change of location present in app.config file with the location present in shared.config file ,with button click. Thanks in advance.

推荐答案

这是更新app.config文件的代码.
修改app.config文件后,如果重新启动应用程序,更改将反映出来
您可以像这样调用函数

this is the code to update the app.config file.
once you modify the app.config file the changes will reflect if you restart the application
you can call the function like this

AppSettingsUpdater.UpdateConfigFile("loginid", "newdetails");
                                AppSettingsUpdater.UpdateConfigFile("pwd", newdetails2);



您的代码应该是这样的




your code should be like this


public class AppSettingsUpdater
   {
       public static void UpdateConfigFile(string key, string value)
       {
           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
           foreach (XmlElement element in xmlDoc.DocumentElement)
               if (element.Name == "appSettings")
               {
                   foreach (XmlNode node in element.ChildNodes)
                       if (node.Attributes[0].Value == key)
                           node.Attributes[1].Value = value;
               }
           xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
           ConfigurationManager.RefreshSection("appSettings");
       }
   }





您的app.config文件应该是这样的






your app.config file should be like this


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="loginid" value="NODATA"></add>
    <add key="pwd" value="NODATA"></add>
  </appSettings>
</configuration>


这篇关于如何从另一个更新一个app.config文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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