0

我正忙于构建一个快速的小型 WinForms 应用程序,该应用程序允许编辑提供的 app.config 文件。我在类周围创建了一个包装器System.Configuration.Configuration,只公开我想要更改的属性。我已经完成AppSettingsConnectionStrings(使用SqlConnectionStringBuilder),现在我正在进入system.net/mailSettings.

这是我当前结构的要点:

public class ServerConfigFile : ConfigFile
{
    ...
    [Category("Database Connection Settings")]
    [DisplayName("Connection String")]
    [RefreshProperties(RefreshProperties.All)]
    [Description("The connection string used to connect to the datasource. Default is \"(LocalDB)\\v11.0\"")]
    public ConnectionStringBuilderFacade ConnectionString { get; private set; }
    ...

    protected override void ReloadProperties()
    {
        this.ConnectionString = new ConnectionStringBuilderFacade(this.UnderlyingConfig.ConnectionStrings.ConnectionStrings["EntitiesContainer"]);
        ...
        this.MailSettings = this.UnderlyingConfig.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
    }
}

public abstract class ConfigFile
{
    protected Configuration UnderlyingConfig { get; private set; }
    ...
    public void RefreshFromFile(string exeFile)
    {
        this.UnderlyingConfig = ConfigurationManager.OpenExeConfiguration(exeFile);
        this.ReloadProperties();
    }

    protected abstract void ReloadProperties();
}

我已经能够从配置文件中获取 MailSettings:

this.MailSettings = this.UnderlyingConfig.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

但由于这是一个快速应用程序,我还没有准备好花时间为一小部分写出整个 TypeConverter 和 UITypeEditor。


可以看出需要的是——smtp设置、投递方式、取货地点(如果指定投递方式的目录)、ssl、用户名、密码……

我的问题:是否有任何现有的 MailSettings 的 PropertyGrid 编辑器可以即插即用,还是我必须硬着头皮推出自己的,或者你们有没有更好的解决方案?

4

1 回答 1

0

所以我最终推出了我自己的拍打解决方案。我将类中的属性映射MailSettingsSectionGroup到我自己的配置类并使用它运行。如下所示:

[Browsable(false)]
public MailSettingsSectionGroup MailSettings { get; private set; }

[Category(MailSettingsCategory)]
[DisplayName("Pickup Directory Location")]
[RefreshProperties(RefreshProperties.All)]
[Description("The folder where to save email messages to be processed by an SMTP server.")]
[Editor(typeof(FolderNameEditor), typeof(UITypeEditor))]
public string SmtpPickupDirectoryLocation
{
    get
    {
        return this.MailSettings.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation;
    }

    set
    {
        this.MailSettings.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation = value;
    }
}
...

输出:

邮件设置配置

于 2016-01-28T07:53:36.093 回答