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

 

相关文章:

  • 2021-11-27
  • 2022-12-23
  • 2021-04-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-25
  • 2022-12-23
相关资源
相似解决方案