我正在尝试从 Windows 窗体保存到 app.config 中的以下设置
<system.net>
<mailSettings>
<smtp from="restore@example.com">
<network host="smtp" port="25" defaultCredentials="true"/>
</smtp>
</mailSettings>
</system.net>
我正在尝试使用下面的代码来设置它,但是我收到配置是只读的错误。
private void SaveMailSettings()
{
var smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
try
{
if (smtpSection == null) return;
smtpSection.Network.Host = txtSmtpHost.Text;
smtpSection.Network.Port = Convert.ToInt32(txtSmtpPort.Text);
smtpSection.From = txtSmtpFrom.Text;
if (smtpSection.Network.UserName != null) smtpSection.Network.UserName = txtSmtpUserName.Text;
if (smtpSection.Network.Password != null) smtpSection.Network.Password = txtSmtpPassword.Text;
Logger.Log(string.Format("Host: {0}, Port: {1}, From: {2}, UserName: {3}, Password: {4}", smtpSection.Network.Host, smtpSection.Network.Port, smtpSection.Network.UserName, smtpSection.Network.Password), "INFO");
}
catch (Exception ex)
{
Logger.Log(ex.Message, "ERROR:");
}
}
有什么办法可以更新 App.Config 文件 mailSettings?