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");
  • 相关阅读:
    oracle表解锁
    pl/sql 如何配置连接远程一个或多个数据库
    Hibernate通过自编写Sql修改
    Hibernate通过自编写sql查询
    java生成临时令牌和访问令牌
    java生成字母首位8位随机码
    [C] 创建目录_mkdir()函数
    [C] 判断目录 / 文件是否存在access()函数
    [C] getopt使用说明
    [C] Segmentation fault (core dumped)
  • 原文地址:https://www.cnblogs.com/94cool/p/9312145.html
Copyright © 2011-2022 走看看