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