[英]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 :
路径有两种类型:
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.