简体   繁体   English

将ListView中选择的文件复制/移动到通过FolderBrowserDialog选择的文件夹失败

[英]Copy/move the files selected in the ListView to the folder selected via FolderBrowserDialog failed

help is really needed) In listView2 there is a list of files which, by clicking on button5, should be copied / moved to the folder selected in FolderBrowserDialog. 确实需要帮助)在listView2中有一个文件列表,通过单击button5,应该复制/移动到FolderBrowserDialog中选择的文件夹。

Here is what it is: 这是它的内容:

private void button5_Click(object sender, EventArgs e)
{
    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
    {
        foreach (ListViewItem item in listView2.SelectedItems)
        {
            if (!File.Exists(Path.GetFullPath(item.Text)))
                return;
            File.Copy(Path.GetFullPath(item.Text), 
                Path.Combine(folderBrowserDialog1.SelectedPath, Path.GetFileName(item.Text)),
                true);
        }
    }
}

the problem is that the files are not copied to the folder 问题是文件没有复制到该文件夹

It seems you are giving the wrong path to your instructions because you are manipulating wrong type of paths. 您似乎正在给出错误的指令路径 ,因为您正在操纵错误类型的路径。 There are two types of path : 路径有两种类型:

  • Absolute path : full path to the file, containing drive and all folders to it, pros are you can access the same path from EVERYWHERE 绝对路径 :文件的完整路径,包含驱动器和所有文件夹,专业人员可以从EVERYWHERE访问相同的路径
  • Relative path : path relative to the executable, if exe is located in "C:/Temp", you can access "C:/Temp/foo/bar" by using a relative path like "foo/bar" and it will work since you are in "C:/Temp" 相对路径 :相对于可执行文件的路径,如果exe位于“C:/ Temp”中,则可以使用“foo / bar”之类的相对路径访问“C:/ Temp / foo / bar”,它将起作用你在“C:/ Temp”

In your case, you choose a directory from FolderBrowserDialog which will return an absolute path like : 在您的情况下,您从FolderBrowserDialog中选择一个目录,它将返回一个绝对路径,如:

folderBrowserDialog1.SelectedPath = "C:/users/myname/mydir"

Then you will iterate through your list which should contain absolute path of your files. 然后,您将遍历列表,其中应包含文件的绝对路径。

Working with absolute paths, you don't need to combine any path, just use them 使用绝对路径,您不需要组合任何路径,只需使用它们

foreach (ListViewItem item in listView2.SelectedItems)
{
    if (!File.Exists())
        return;

    File.Copy(item.Text, folderBrowserDialog1.SelectedPath, true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM