Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Could not find part of the path" error when copying a file

I've googled about this all over the Internet and still haven't found a solution. As an ultimate try, I hope someone can give me an exact answer.

I get that error when I try to copy a file from a directory to another in an File Explorer I'm trying to do on my own. It has a treeview control to browse for directories and a listview control to display the contents of the directory. This is how the code would look like, partially:

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
    {
        sourceDir = treeView1.SelectedNode.FullPath;
        for (int i = 0; i < listView1.SelectedItems.Count; ++i)
        {
            ListViewItem l = listView1.SelectedItems[i];
            toBeCopied[i] = l.Text; // string[] toBeCopied, the place where I save the file names I want to save
        }
    }


private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
    {
        targetDir = treeView1.SelectedNode.FullPath;
        try
        {
            for (int i = 0; i < toBeCopied.Length; ++i)
            {
                File.Copy(sourceDir + "\\" + toBeCopied[i], targetDir + "\\" + toBeCopied[i], true);
                refreshToolStripMenuItem_Click(sender, e);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + Environment.NewLine + ex.TargetSite);
        }
    }

The place where I got the error is at File.Copy(sourceDir + "\\" + toBeCopied[i] ....

I've read that it could be something that has to do with the mapping of devices, but I don't really know what that is.

like image 732
Ciprian Tomoiagă Avatar asked Jun 06 '11 21:06

Ciprian Tomoiagă


1 Answers

Can you take a look at the Path.Combine method on MSDN? This will help make sure all your entire path doesn't have extra \'s where they shouldn't be.

i.e. Path.Combine(sourceDir, toBeCopied[i])

If you are still getting an error, let me know what the value if the above.

like image 155
LowTide Avatar answered Sep 22 '22 00:09

LowTide