-2

Using File.OpenDialog how can i make a copy of selected files to a certain (predeclared or even better from a string variable taken from textbox) location? I assume i can firstly simply use the ofd method, but where to determine the location to copy?

InitializeComponent();

PopulateTreeView();

this.treeView1.NodeMouseClick +=
    new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);

OpenFileDialog ofd1 = new OpenFileDialog();

and for the button:

private void button3_Click(object sender, EventArgs e)
{
    if (ofd1.ShowDialog() == DialogResult.OK)
    { }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
xsairo
  • 1
  • 3
  • I just dont know how to execute an operation to copy selected files after if (ofd1.ShowDialog() == DialogResult.OK) – xsairo Jun 10 '15 at 03:16
  • 1
    Did you do any research at all? Did you try searching for "[copy file .net"](https://msdn.microsoft.com/en-us/library/system.io.file.copy.aspx)? – John Saunders Jun 10 '15 at 04:06
  • If I understand correctly, you are using `OpenFileDialog` to choose the files to copy, correct? I suggest that you look at the documentation for `OpenFileDialog` to find the methods you can use to get the selected file names. From there, do a google search on how to copy a file in .NET. – Code-Apprentice Jun 10 '15 at 04:08

2 Answers2

0

If I understood correctly, then once you select the file from open file dialog, you want to copy it to a certain location. Then you can use something like this code -

        if (this.openFileDialog1.ShowDialog()== System.Windows.Forms.DialogResult.OK)
        {
            var fileName = this.openFileDialog1.FileName;
            File.Copy(fileName, "DestinationFilePath");
        }

Or in case of multiple selected file, something like this -

        if (this.openFileDialog1.ShowDialog()== System.Windows.Forms.DialogResult.OK)
        {
            var fileNames = this.openFileDialog1.FileNames;

            foreach (var fileName in fileNames)
            {
                File.Copy(fileName, "DestinationFilePath/" + fileName);
            }
        }
Yogi
  • 9,174
  • 2
  • 46
  • 61
-1

Check out the foreach loop in this for iterating through all of the files you selected using the OpenDialog.

I think this is what you're looking for in regards to actually copying the files. It takes a source directory and copies to the destination you provide.

Community
  • 1
  • 1
TheMi7ch
  • 11
  • 3