Reputation: 149
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
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:
If that is correct, you're close. Some things to note:
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);
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.
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:
textBox1
is present in the directory.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
.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
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