我正忙着构建一个快速的小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设置,传递方法,拾取位置(如果传递方法是specifiedDirectory),ssl,用户名,密码.
我的问题是:是否有任何现有的PropertyGrid编辑器,我可以即插即用,或者我必须咬紧牙关,推出自己的,或者说,你们有一个更好的解决方案给我吗?
发布于 2016-01-28 07:53:36
所以我最终推出了自己的解决方案。我将MailSettingsSectionGroup类中的属性映射到自己的config类,并使用它运行。如下所示:
[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;
}
}
...产出:

https://stackoverflow.com/questions/34977307
复制相似问题