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