访问应用程序配置文件会产生错误

本文关键字:错误 应用程序 配置文件 访问 | 更新日期: 2023-09-27 18:20:54

我必须以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>

当我以这种方式访问时。这表明应用程序在以下上下文中不存在。请帮忙。

访问应用程序配置文件会产生错误

转到项目属性->设置添加您的属性(如用户名和密码)。

如果您希望属性可以从配置文件更改,请将属性的范围设置为"应用程序"。

在像这样的代码使用中:

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();

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

尝试使用Properties.Settings类访问app.config文件,检查Windows窗体的应用程序设置