c#操作appsettiongs

时间:2022-03-01 21:58:18

try

{

//指定要修改的配置文件的路径

Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");

config.AppSettings.Settings["MailServer"].Value = txtMailServer.Value.Trim();

config.AppSettings.Settings["MailAddress"].Value = txtMailAddress.Value.Trim();

config.AppSettings.Settings["MailUser"].Value = txtMailUser.Value.Trim();

config.AppSettings.Settings["MailPwd"].Value = txtMailPwd.Value.Trim();

config.Save();

//刷新配置节点

ConfigurationManager.RefreshSection("appSettings");

}

catch (Exception)

{

Literal alerts = new Literal();

alerts.Text = "<script>alert('更新配置文件中的邮件服务器参数失败,请确保有足够的权限!');</script>";

Page.Controls.Add(alerts);

}