将配置文件 MailSettings 添加到 PropertyGrid
Add config file MailSettings to PropertyGrid
我正忙于构建一个快速的小 WinForms 应用程序,它允许编辑提供的 app.config 文件。我围绕 System.Configuration.Configuration
class 创建了一个包装器,仅公开我想要更改的属性。我已经完成了 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 编辑器我可以即插即用,或者我是否必须硬着头皮推出我自己的,或者你有吗好人有更好的解决方案吗?
所以我最终推出了自己的综合解决方案。我将 MailSettingsSectionGroup
class 中的属性映射到我自己的配置 class 并且只是 运行 。类似下面的内容:
[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;
}
}
...
输出:
我正忙于构建一个快速的小 WinForms 应用程序,它允许编辑提供的 app.config 文件。我围绕 System.Configuration.Configuration
class 创建了一个包装器,仅公开我想要更改的属性。我已经完成了 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 编辑器我可以即插即用,或者我是否必须硬着头皮推出我自己的,或者你有吗好人有更好的解决方案吗?
所以我最终推出了自己的综合解决方案。我将 MailSettingsSectionGroup
class 中的属性映射到我自己的配置 class 并且只是 运行 。类似下面的内容:
[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;
}
}
...
输出: