开发者

"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.Tex开发者_C百科t; // 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.


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.


Does the target path up to the file name exist? File.Copy() will not create any missing intermediate path, you would need to do this yourself. Use the debugger to see both the source and target paths you are creating and make sure the source exists and the target exists at least up to the parent of the target file.


You do not show where toBeCopied is created. It looks like you are probably running past the end of the values that are set in the click event, and trying to copy a bunch of files with empty names.

You should add this to the beginning of your click event

toBeCopied = new string[listView1.SelectedItems.Count];

Also (as others have noted) instead of

sourceDir + "\\" + toBeCopied[i]

you should use

Path.Combine(sourceDir, toBeCopied[i])


Assuming both sourceDir and targetDir exist (which you can and should check), you might be doubling up a trailing \. When building paths, you should use Path.Combine.

File.Copy(Path.Combine(sourceDir, toBeCopied[i]), Path.Combine(targetDir, toBeCopied[i]), true);


Borrowing from Henk's loop, but I'd add the file & directory checks, since it is the path not found errors that need checking/creating that the OP has the problem with.

for (int i = 0; i < toBeCopied.Length; ++i)
{
    string sourceFile = Path.Combine(sourceDir, toBeCopied[i]);
    if(File.Exists(sourceFile))
    { 
        string targetFile = Path.Combine(targetDir, toBeCopied[i]);
        if(!Directory.Exists(targetDir)) 
            Directory.CreateDirectory(targetDir); 
        File.Copy(sourceFile, targetFile, true);
    }
    refreshToolStripMenuItem_Click(sender, e)
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜