在 .net wf 应用程序中保存到 App.Config 中的 mailSettings
Saving to mailSettings in App.Config in .net wf application
我正在尝试将 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 吗?
您无法通过 ConfigurationManager.GetSection
编辑配置文件
您应该打开文件并进行更改并保存文件。
注意:不要在调试模式下测试它。我不知道为什么在调试模式下更改不保存。但是正常情况下 运行 .exe 文件更改已保存。
var webConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings =(MailSettingsSectionGroup)webConfig.GetSectionGroup("system.net/mailSettings");
webConfig.AppSettings.Settings["test"].Value = "asdad";
SmtpSection smtp = settings.Smtp;
SmtpNetworkElement net = smtp.Network;
net.Port = 2005;
net.Host = "localhostEditedEdited";
webConfig.Save(ConfigurationSaveMode.Modified);
我正在尝试将 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 吗?
您无法通过 ConfigurationManager.GetSection
您应该打开文件并进行更改并保存文件。
注意:不要在调试模式下测试它。我不知道为什么在调试模式下更改不保存。但是正常情况下 运行 .exe 文件更改已保存。
var webConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings =(MailSettingsSectionGroup)webConfig.GetSectionGroup("system.net/mailSettings");
webConfig.AppSettings.Settings["test"].Value = "asdad";
SmtpSection smtp = settings.Smtp;
SmtpNetworkElement net = smtp.Network;
net.Port = 2005;
net.Host = "localhostEditedEdited";
webConfig.Save(ConfigurationSaveMode.Modified);