码迷,mamicode.com
首页 > 移动开发 > 详细

修改App.config的键和值

时间:2020-06-28 22:20:23      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:strong   exe   内容   color   node   system   tde   ring   load   

App.config中内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <!--  <system.windows.forms jitDebugging="true"  //允许调试     />-->
  <appSettings>
    <add key="setInterval" value="1000"/>
    <add key="chooseCOM" value="COM4"/>
    <add key="senStr" value="R"/>
  </appSettings>
</configuration>

读取App.config中chooseCOM内容,并把COM4改为COM1:

string setCOM = ConfigurationSettings.AppSettings["chooseCOM"];
  ConfigHelper.SetValue("chooseCOM","  COM1");//

  

设置app.config键值的方法:

 public static class ConfigHelper
    { 
        public void SetValue(String AppKey, String AppValue)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
            XmlNode xNode;
            XmlElement xElem1;
            XmlElement xElem2;
            xNode = xDoc.SelectSingleNode("//appSettings");
            xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key=‘" + AppKey + "‘]");
            if (xElem1 != null)
                xElem1.SetAttribute("value", AppValue);
            else
            {
                xElem2 = xDoc.CreateElement("add");
                xElem2.SetAttribute("key", AppKey);
                xElem2.SetAttribute("value", AppValue);
                xNode.AppendChild(xElem2);
            }
            xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
        }
    }

 

修改App.config的键和值

标签:strong   exe   内容   color   node   system   tde   ring   load   

原文地址:https://www.cnblogs.com/tiancaige/p/13205161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!