zoukankan      html  css  js  c++  java
  • c# 修改xml格式config文件

    xml 格式的config文件如下:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="port" value="8046" />    
        <add key="serverUrl" value="http://****:8046" />
      </appSettings> 
    </configuration>

     修改:

    exePath文件路径

    string exePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "{程序名称}.exe");

     Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
                    if (config.AppSettings.Settings["port"] == null)
                    {
                        config.AppSettings.Settings.Add("port", port);
                    }
                    else
                    {
                        var portName = config.AppSettings.Settings["port"].Value;
                        config.AppSettings.Settings["port"].Value = port;
                    }
                    if (config.AppSettings.Settings["serverUrl"] == null)
                    {
                        config.AppSettings.Settings.Add("serverUrl", remoteUrl);
                    }
                    else
                    {
                        var _serverUrl = config.AppSettings.Settings["serverUrl"].Value;
                        config.AppSettings.Settings["serverUrl"].Value = remoteUrl;
                    }
                    config.Save(ConfigurationSaveMode.Modified);//保存
                    ConfigurationManager.RefreshSection("appSettings");
  • 相关阅读:
    回到顶部
    angularjs 复选框 单选框
    关于angularjs的ng-repeat指令
    JS字符串对象
    JS的控制语句与异常
    JS的运算符
    JS的引入方式和基础规范
    z-index及透明度opacity,利用overflow设置头像
    css的定位
    css的float属性及清除浮动
  • 原文地址:https://www.cnblogs.com/94cool/p/9312145.html
Copyright © 2011-2022 走看看