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");

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM