C# - setting a user defined folder path as a string variable the first time a program is run

user2258854 picture user2258854 · Apr 8, 2013 · Viewed 14.4k times · Source

Thanks to you all for the help so far! I am extremely new to c# and code in general. I have a question that I cannot seem to find the answer to.

I just wrote a simple program that moves files from one folder to a new folder named that day's date. Please see below:

    private void button1_Click(object sender, EventArgs e)
    {

            DateTime now = DateTime.Now;
            string date = (now.ToString("D"));

            string a = @"m:\\staff docs\\faxes\\";
            string b = @a + date + "\\";
            System.IO.Directory.CreateDirectory(b);


        DirectoryInfo dir1 = new DirectoryInfo("c:\\blah");
        DirectoryInfo dir2 = new DirectoryInfo("@b");

        FileInfo[] DispatchFiles = dir1.GetFiles();
        if (DispatchFiles.Length > 0)
        {
            foreach (FileInfo aFile in DispatchFiles)
            {
                string files = @b + aFile.Name;
                int count = 0;
            Find :
                if (File.Exists(files))
                {
                    files = files + "(" + count.ToString() + ").txt";
                    count++;
                    goto Find;
                }
                aFile.MoveTo(files);
            }
        }   
    {
        MessageBox.Show("Your files have been moved!");

I'd like to have the user define the source folder variable and the destination folder variable, either by having them navigate to the folder in a file browser, or a Console.ReadLine - but not every time they run the program, just the first. It would be ideal if they could change the path if they wanted to later on as well.

Many thanks!

EDIT

My solution was a Button on my Form that calls this block:

private void button3_Click(object sender, EventArgs e)
{
 FolderBrowserDialog fbd = new FolderBrowserDialog();
        fbd.Description = "Select source folder";
        fbd.ShowDialog();
        string Source = fbd.SelectedPath;
        Properties.Settings.Default.source = Source;
        Properties.Settings.Default.Save();

        FolderBrowserDialog fbd2 = new FolderBrowserDialog();
        fbd2.Description = "Select destination folder";
        fbd2.ShowDialog();
        string d1 = fbd2.SelectedPath;
        string d2 = "\\";
        string Destination = d1 + d2;
        Properties.Settings.Default.destination = Destination;
        Properties.Settings.Default.Save();
}

Answer

DarkSquirrel42 picture DarkSquirrel42 · Apr 8, 2013

you can use the "user settings" described here: http://msdn.microsoft.com/en-us/library/bb397750.aspx

//EDIT:

let's say You have a USER Setting of type string called "MySetting"

if you want to read it:

var someVar = Properties.Settings.Default.MySetting;

if you want to write it (assuming your data is in someVar):

Properties.Settings.Default.MySetting = someVar;
Properties.Settings.Default.Save();

the call to Save() persists your changes ... the changes are bound to the windows user account

at design time your setting should be defined as Scope USER