“找不到部分路径”复制文件时出错

发布于 2024-11-14 07:31:05 字数 1230 浏览 6 评论 0原文

我在互联网上搜索了这个问题,但仍然没有找到解决方案。作为最终的尝试,我希望有人能给我一个确切的答案。

当我尝试自己尝试在文件资源管理器中将文件从一个目录复制到另一个目录时,我收到该错误。它有一个用于浏览目录的树视图控件和一个用于显示目录内容的列表视图控件。这就是代码的部分外观:

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);
        }
    }

我收到错误的地方位于 File.Copy(sourceDir + "\\" + toBeCopied[i] ...

我读过它可能与设备映射有关,但我真的不知道那是什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

丑疤怪 2024-11-21 07:31:05

您能看一下 MSDN 上的 Path.Combine 方法吗?这将有助于确保您的整个路径没有多余的 \ 。

Path.Combine(sourceDir, toBeCopied[i])

如果您仍然收到错误,请告诉我上述值是什么。

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.

说好的呢 2024-11-21 07:31:05

到文件名的目标路径是否存在? File.Copy() 不会创建任何缺失的中间路径,您需要自己执行此操作。使用调试器查看您正在创建的源路径和目标路径,并确保源存在且目标至少存在到目标文件的父级。

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.

挽心 2024-11-21 07:31:05

您没有显示 toBeCopied 的创建位置。看起来您可能已经超出了单击事件中设置的值的末尾,并尝试复制一堆名称为空的文件。

您应该将其添加到点击事件的开头

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

此外(正如其他人指出的那样)而不是

sourceDir + "\\" + toBeCopied[i]

您应该使用

Path.Combine(sourceDir, toBeCopied[i])

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])
回忆凄美了谁 2024-11-21 07:31:05

假设 sourceDirtargetDir 都存在(您可以并且应该检查),您可能会加倍尾随的 \。构建路径时,应使用 Path.Combine

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

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);
银河中√捞星星 2024-11-21 07:31:05

借用 Henk 的循环,但我会添加文件 &目录检查,因为这是 OP 有问题的需要检查/创建的路径未找到错误。

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)
}

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)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文