Question

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();
}
Was it helpful?

Solution

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

OTHER TIPS

There are a few possibilities you can use.
One thing would be to create an XML-file the first time the user entered the paths. You could check for the existance of the file and if it exists, read from it and if not exists, create it and write data to it. Of course you are capable of editing your XML-file.

There is the System.Xml.XmlDocument-class

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

    string inputpath = "C:\....";

    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();

    //Create a new node and add it to the document.
    //The text node is the content of the price element.
    XmlElement elem = doc.CreateElement("Inputpath");
    XmlText text = doc.CreateTextNode(inputpath);
    doc.DocumentElement.AppendChild(elem);
    doc.DocumentElement.LastChild.AppendChild(text);

    doc.Save(Console.Out);

  }
}

See here for a reference.

You could write a value into the registry, too. Just as a possibility.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top