C# 访问应用程序配置文件会创建错误

C# 访问应用程序配置文件会创建错误,c#,winforms,app-config,C#,Winforms,App Config,我必须以app.config MailSettingsSectionGroup mailSettings =App.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup; if (mailSettings != null) { FromMail = mailSettings.Smtp.Network.UserName;

我必须以
app.config

    MailSettingsSectionGroup mailSettings =App.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

 if (mailSettings != null)
            {
                 FromMail = mailSettings.Smtp.Network.UserName;
                 password = mailSettings.Smtp.Network.Password;
            }
应用程序配置文件为

 <system.net>
    <mailSettings>
      <smtp from="abc@email.com">
        <network host="smtp.server.com" port="587" userName="abc@email.com" password="password" enableSsl="true" />
      </smtp>
    </mailSettings>
  </system.net>


当我以这种方式访问时。显示应用程序在以下上下文中不存在。请提供帮助。

尝试使用Properties.Settings类访问App.config文件,请选中转到项目属性->设置添加您的属性(如用户名和密码)

如果您想更改配置文件中的属性,请将属性范围设置为“应用程序”

在代码中,使用如下方式:

FromMail = Properties.Settings.Default.UserName;
password = Properties.Settings.Default.Password;
您可以通过以下方式更改GUI中的属性值:

Properties.Settings.Default.UserName = FromMail;
Properties.Settings.Default.Password = password;
Properties.Settings.Default.Save();

您只能更改具有用户作用域的属性。

我缺少声明,但我已在网上搜索到。如何声明它?先生,是否可以从Gui更改这些设置值,或者如果我再进行一次表单名称设置,是否可以更改它?是的,您可以更新每个属性并保存设置<代码>属性.Settings.Default.UserName=“value”;Properties.Settings.Default.Save()您可以使用
Properties.settings.Default.load()再次加载设置