我正在忙着构建一个快速的小型WinForms应用程序,该应用程序允许编辑提供的app.config文件。我在System.Configuration.Configuration
类周围创建了一个包装器,只显示了我想要更改的属性。我已完成AppSettings
和ConnectionStrings
(使用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编辑器,我可以即插即用,或者我必须咬紧牙关并推出我自己的,或者你好人们有一个均匀的对我来说更好的解决方案?
答案 0 :(得分: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;
}
}
...
输出: