【问题标题】:Accessing App Config file creates an error访问 App Config 文件会产生错误
【发布时间】:2013-07-04 14:57:53
【问题描述】:

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

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

【问题讨论】:

  • 那是我错过了声明,但我已经在网上搜索过了。如何申报?

标签: c# winforms app-config


【解决方案1】:

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

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

在代码中这样使用:

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 范围的属性。

【讨论】:

  • 先生是否可以从 Gui 更改这些设置值,或者如果我再做一个表单名称设置是否可以更改它??
  • 是的,您可以更新每个属性并保存设置。 Properties.Settings.Default.UserName="value"; Properties.Settings.Default.Save(); 您可以使用 Properties.Settings.Default.load() 再次加载设置
【解决方案2】:

尝试使用Properties.Settings类访问app.config文件,检查Application Settings for Windows Forms

【讨论】:

    猜你喜欢
    • 2018-11-19
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    相关资源
    最近更新 更多