Search code examples
c#propertygridconfigurationmanagermailsettings

Add config file MailSettings to PropertyGrid


I'm busy building a quick little WinForms app that allows editing of a provided app.config file. I created a wrapper around the System.Configuration.Configuration class, exposing only the properties I want changed. I've done AppSettings and ConnectionStrings (using SqlConnectionStringBuilder) and now I'm moving onto system.net/mailSettings.

Here's the gist of my current structure:

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();
}

I've been able to source the MailSettings from the config file:

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

but since this was meant to be a quick app, I'm not quite ready to invest the time to write out a whole TypeConverter and UITypeEditor just for one small section.


It can be seen that what's needed are - smtp settings, delivery methods, pickup locations (if delivery method is specifiedDirectory), ssl, username, password...

My question: is there any existing PropertyGrid editor for MailSettings that I can plug and play, or do I have to bite the bullet and roll out my own, or do you fine people have an even better solution for me?


Solution

  • So I ended up rolling out my own slapped together solution. I mapped the properties in the MailSettingsSectionGroup class to my own config class and just ran with it. something like the below:

    [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;
        }
    }
    ...
    

    The output:

    Mail Settings config