Joe Pearson
Joe Pearson

Reputation: 149

Copy entire directory with button from two dynamic locations

Okay, I have a button (button1) which I want to copy the static directory to the chosen directory. Essentially I have textbox1 in which different numeric values are added which correlate with different directories. I have a dictionary that sets the string to mapping which links to codes from textbox2 to the path of the origination folder. . This determines where we copy our data from. I want this data to then be copied into the folder selected in textbox2 through the folderBrowserDialog1.ShowDialog(); command. how to i create the dictionary and where do i put it for textbox1, and how do i then get the button to take whatever is in textbox1 and copy the entire directory to textbox2?

private Dictionary<string, string> mapping = new Dictionary<string, string>
{
    { "111", @"C:\Program Files\Example" },
    { "112", @"C:\Program Files\Example2" },
    { "113", @"C:\Program Files\Example3" },
};

public static string[] GetFiles(string mapping);

public static void Copy(string sourceFileName, string destFileName);

private void button2_Click(object sender, EventArgs e)
{

    string destination = textBox1.Text;

    foreach (var f in Directory.GetFiles(mapping))
    {
        File.Copy(Path.Combine(mapping, f)); destination;
    }
}

Upvotes: 0

Views: 404

Answers (2)

Tim
Tim

Reputation: 28530

I'm not 100% sure I understood the details of your question, as the uses of TextBox1 and TextBox2 don't seem consistent, but here's what I read:

  • TextBox1 has the codes that the dictionary maps to a source directory.
  • TextBox2 has the path (from the dialog box) to the destination directory.

If that is correct, you're close. Some things to note:

  1. I'm not sure why you have these two lines. They look like method definitions, but there's no implementation. I think you can remove them and use the equivalent System.IO calls:

    public static string[] GetFiles(string mapping);

    public static void Copy(string sourceFileName, string destFileName);

  2. Directory.GetFiles(mapping) won't work, because mapping is a Dictionary<string, string>, not a string. You need to select the corresponding value (path) based on the key (numeric code) from TextBox1 and use that in the Directory.GetFiles() method.

  3. File.Copy(Path.Combine(mapping, f)); destination; is an incorrect syntax and won't compile (should be File.Copy(Path.Combine(mapping, f), destination);. Additionally, you don't need to combine the source path and filename for the first argument, as GetFiles returns the path along with the filename (including extension). You will need to get the filename alone and combine it with the sourece path for the destination file, however. You can use Path.GetFileName to do this.

Try this code below - it addresses the issues noted above, with the assumption being that textBox1 is the source in mappings and textBox2 is the destination from the dialog window:

private void button2_Click(object sender, EventArgs e)
{

    string source = textBox1.Text
    string destination = textBox2.Text;

    if (mappings.ContainsKey(source))
    {
        foreach (var f in Directory.GetFiles(source))
        {
            File.Copy(f, Path.Combine(destination, Path.GetFileName(f)));
        }
    }
    else
    {
        // No key was found, how you handle it will be dictated by the needs of the application and/or users.
    }
}

The above code does the following:

  1. Gets the numeric value for the path of the source directory. This should correspond to one of the keys in the dictionary.
  2. Gets the path for the destination directory based on the user selected value from the dialog box.
  3. Checks to see if the key from textBox1 is present in the directory.
  4. If it is, it gets the corresponding value for that key, which is the path for the source directory.
  5. Next, it loops through the files in the specified source directory, copying each one in turn to the destination directory. Note that f (the file) is used as the source file for the first argument (as it contains the path as well, as Directory.GetFiles() returns both the path and the filename), and the destination directory is combined with the filename (retrieved by a call to Path.GetFileName). If f alone was used in the Path.Combine() method, you'd wind up with sourcePath\originalPath\filename.
  6. If the key wasn't found, you ignore it or do something with it.

Again, the above code is based on my understanding of your question. If my understanding was less than correct, the principles I outlined should still be able to assist you in resolving the issue.

** EDIT **

Per the comments below, it sounds like all you need to do is flip the assignments for source and destination. textBox2 will contain the code that corresponds to a key in the mappings directory, which will give you the path of the source directory. textBox1 will contain the destination path. Like this:

string source = textBox2.Text
string destination = textBox1.Text;

The rest of the code stays the same.

Upvotes: 0

DrKoch
DrKoch

Reputation: 9772

Here is an answeer to "How copy an entire directory of files":

Use Directory.GetFiles() (see documentation) to get a list of all files in a directory.

Then use File.Copy() (see documenation) to copy a single file:

foreach(var f in Directory.GetFiles(srcPath))
{
    File.Copy(Path.Combine(srcPath, f), dstPath);
}


EDIT

Directory.GetFiles() requires a path:

private void button2_Click(object sender, EventArgs e)
{
    string destination = textBox1.Text;
    string srcdir = mapping["111"];
    foreach(var f in Directory.GetFiles(srcdir))
    {
         string srcpath = Path.Combine(srcdir, f)
         File.Copy(srcpath, destination);
    }
}

Upvotes: 1

Related Questions