Save FolderBrowserDialog selection to file (config.txt)?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Save FolderBrowserDialog selection to file (config.txt)?

  1. #1
    Join Date
    May 2014
    Posts
    6

    Save FolderBrowserDialog selection to file (config.txt)?

    I've been reading guides on using the settings designer to accomplish this. However I have no clue where to place the code in my program to save/load the settings. Most of the guides seem to refer to VS 2005. I'm using VS 2013 and things are a bit different. The guides also assume you have previous programming experience, which I do not.

    Here's my Form1.cs file:

    http://pastebin.com/xMG1MKi3

    Lines 113-126 is the directory selection button. I need to save the selection to a config file, preferably like most programs store settings, using a config.txt file in the "%USERPROFILE%\My Documents\Program Name" directory which can be opened and modified by the user, and read by the program when it starts.
    Last edited by SZ87; May 12th, 2014 at 04:14 PM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,197

    Re: Save FolderBrowserDialog selection to file (config.txt)?

    It's real simple to use the built-in Settings functionality.

    To add a setting, just right click on your forms project in the solution explorer and choose, "Properties".
    On the left, click on the Settings tab.
    Enter a new setting (I called mine 'MySavedFolder').
    Choose the type and visibility (User or application). I chose User.
    Give the setting a default value (I used 'default value').

    Next, wire up the setting in the forms code. I created a simple test form with an textbox and a save button.
    In the form editor, double click on the button to create a button handler.

    Next, wire up the setting.
    Code:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
            // Retrieve the settings and load it into the textbox
            textBox1.Text = Properties.Settings.Default.MySavedFolder;
        }
    
        private void saveButton_Click(object sender, EventArgs e)
        {
            // Save the value from the textbox into the settings.
            Properties.Settings.Default.MySavedFolder = textBox1.Text;
            Properties.Settings.Default.Save();
        }
    }
    To test this out, when you first open the form, you'll see the value appear in the textbox as "default value".
    Next, type in something into the textbox (e.g. "my new value").
    Click the save button which will save the setting.
    Close the application and then reopen it.
    Notice that the textbox now appears with "my new value".

    In your case, you'll want to use the BrowseForFolder to prompt the user for a folder, then you'll just save it as a setting like I have done in the button handler code above.

  3. #3
    Join Date
    May 2014
    Posts
    6

    Re: Save FolderBrowserDialog selection to file (config.txt)?

    Arjay, you are a true saint helping me with this and my previous post. I will try this and let you know what happens.

  4. #4
    Join Date
    May 2014
    Posts
    6

    Re: Save FolderBrowserDialog selection to file (config.txt)?

    My sincerest apologies, Arjay. I'm trying to incorporate your example code into my existing form's code, but I'm obviously doing something wrong.

    Take a look at my form now:

    http://i.imgur.com/5t25VdF.png

    As you can see, I've removed "button5" and replaced it with those two controls.

    Here's the code for that button beside the textbox:

    Code:
    private void dirSelect_Click(object sender, EventArgs e)
            {
                var fbd = new FolderBrowserDialog
                {
                    Description = @"Please select the Cinematic Mod installation directory."
                };
    
                if (fbd.ShowDialog() != DialogResult.OK) return;
    
                    Properties.Settings.Default.SelectedPath = textBox1.Text;
                    Properties.Settings.Default.Save();
    
                {
                    _gameDir = fbd.SelectedPath;
                }
            }
    What I was hoping that would do is place the path string the user selected into the text box.

    Like so:

    Before - http://i.imgur.com/P5wz7jA.png
    After - http://i.imgur.com/cc9JAM3.png

    There's also the settings load code here:

    Code:
    public Form1()
            {
                InitializeComponent();
                textBox1.Text = Properties.Settings.Default.SelectedPath;
            }
    In the settings designer, I've got:

    Name = SelectedPath
    Type = string
    Scope = User
    Value= Select the CM installation directory...

    I can't be too far away from getting this to work. I am so tired from working almost nonstop on this for the past 4 days. Something so simple must not be so simple after all.

    Would you like me to ZIP my entire project so you can take a look?

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,197

    Re: Save FolderBrowserDialog selection to file (config.txt)?

    Replace
    Code:
    Properties.Settings.Default.SelectedPath = textBox1.Text;
    with
    Code:
    Properties.Settings.Default.SelectedPath = fbd.SelectedPath;
    Also, it doesn't matter but you have an extra pair of braces that aren't doing anything.

    Here's what the code should look like:
    Code:
    private void dirSelect_Click(object sender, EventArgs e)
    {
        var fbd = new FolderBrowserDialog
        {
            Description = @"Please select the Cinematic Mod installation directory."
        };
    
        if (fbd.ShowDialog() != DialogResult.OK) { return; }
    
        Properties.Settings.Default.SelectedPath = fbd.SelectedPath;
        Properties.Settings.Default.Save();
    
        _gameDir = fbd.SelectedPath;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center