在运行时修改web.config appSettings的方法如下:
修改web.config appSettings的方法如下:
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
appSettings.Settings["key"].Value = "new_value";
config.Save();
$webConfigPath = "C:\inetpub\wwwroot\web.config"
$xml = [xml](Get-Content $webConfigPath)
$xml.configuration.appSettings.add | ? { $_.key -eq "key" } | % { $_.value = "new_value" }
$xml.Save($webConfigPath)
var fs = require('fs');
var xml2js = require('xml2js');
var parser = new xml2js.Parser();
var builder = new xml2js.Builder();
fs.readFile('web.config', function(err, data) {
parser.parseString(data, function(err, result) {
result.configuration.appSettings.add.forEach(function(add) {
if (add.$.key === 'key') {
add.$.value = 'new_value';
}
});
var xml = builder.buildObject(result);
fs.writeFile('web.config', xml, function(err) {
if (err) throw err;
console.log('web.config updated');
});
});
});
注意:在修改web.config文件时,需要注意文件的读写权限,以及在修改后重新加载web.config文件。
领取专属 10元无门槛券
手把手带您无忧上云