Folder browser dialog to remember recent folder. Which method is better? which of the following code you prefer and why?

The scenario is, I need to pass the recent folder path  to folder browser dialog and if user selects any folder, need to save it back. The value is stored in Application settings.

My settings class.

    internal sealed class Settings : ApplicationSettingsBase
    {
        private static readonly Settings DefaultInstance = (Settings)Synchronized(new Settings());

        public static Settings Default
        {
            get { return DefaultInstance; }
        }

        [UserScopedSetting]
        public string RecentFolder
        {
            get { return (string) this["RecentFolder"]; }    
            set
            {
                this["RecentFolder"] = value;
                Save();
            }
        }
}


Method 1.

var dialog = new FolderBrowserDialog
            {                
                SelectedPath = Settings.Default.RecentFolder
            };                

            DialogResult result = dialog.ShowDialog();

            if (result == DialogResult.OK &amp;&amp; dialog.SelectedPath.Length &gt; 0)
            {
                Settings.Default.RecentFolder = dialog.SelectedPath;
               //action goes here               
            }


Method 2: using extension methods.

    public static class FolderBrowserDialogExtensions
    {
//The extension method is in separate project where we don't have reference to the settings class.

        public static DialogResult ShowDialog(this FolderBrowserDialog dialog, ApplicationSettingsBase settings, string key)
        {
            dialog.SelectedPath = settings[key].ToString();

            DialogResult result = dialog.ShowDialog();

            if (result == DialogResult.OK || result == DialogResult.Yes)
            {
                settings[key] = dialog.SelectedPath;
                settings.Save();
            }
            return result;
        }
    }


            var dialog = new FolderBrowserDialog();

            DialogResult result = dialog.ShowDialog(Settings.Default, "RecentFolder");

            if (result == DialogResult.OK &amp;&amp; dialog.SelectedPath.Length &gt; 0)
            {
               //action goes here                
            }
Extensions are good when you want to re-use this logic in multiple places. As it seems to be the case, i would go with the second method. Generally it is a good practice to avoid code duplication where possible.

The only possible issue i see is that those two methods have different behaviours: first method saves settings after checking the length, while the second method does this before checking. I'm not sure if that is important (can you even get an empty string from this dialog if you press "ok"?).

EDIT:

You can also overload this static method to use lambdas. For example:

public static class FolderBrowserDialogExtensions
{
    public static DialogResult ShowDialog(this FolderBrowserDialog dialog, ApplicationSettingsBase settings, string key)
    {
        return dialog.ShowDialog(settings[key].ToString(), 
                                 path =&gt; {
                                             settings[key] = dialog.SelectedPath;
                                             settings.Save();
                                         });
    }

    public static DialogResult ShowDialog(this FolderBrowserDialog dialog, string defaultValue, Action&lt;string&gt; onAccept)
    {
        dialog.SelectedPath = defaultValue;
        var result = dialog.ShowDialog();
        if (result == DialogResult.OK)
        {
            // check for null, if needed
            onAccept(dialog.SelectedPath);
        }
        return result;
    }
}


Then you can stick to your Settings class:

dialog.ShowDialog(Settings.RecentFolder, path =&gt; Settings.RecentFolder = path);