zoukankan      html  css  js  c++  java
  • 修改App.config的键和值

    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");
            }
        }
  • 相关阅读:
    使用arcpy添加grb2数据到镶嵌数据集中
    使用python把gdb格式的文本文件转为utf-8的格式
    Spring Cloud
    windows 下nginx配置php支持
    nginx开启gzip
    ant design 修改tab样式
    使用arcpy替换工程文件中的栅格图层数据源异常
    cenos 安装hadoop
    失败
    linux挂载新硬盘
  • 原文地址:https://www.cnblogs.com/tiancaige/p/13205161.html
Copyright © 2011-2022 走看看