类别:程序开发

日期:2022-06-26 浏览:1720 评论:0

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的键和值
本文链接:https://vtzw.com/post/1019.html
作者授权:除特别说明外,本文由 零一 原创编译并授权 零一的世界 刊载发布。
版权声明:本文不使用任何协议授权,您可以任何形式自由转载或使用。
 您阅读本篇文章共花了: 

 可能感兴趣的文章

评论区

发表评论 / 取消回复

必填

选填

选填

◎欢迎讨论,请在这里发表您的看法及观点。