首页 > 解决方案 > C#将列表框中列出的项目移动到另一个文件夹中

问题描述

谢谢您的帮助!我一直在尝试将文件名列出到列表框中,并将列表框中列出的文件移动到另一个文件夹中,但我做不到。我已经了解了如何将文件列出到 Listbox 中,但我唯一被困在这里的是我无法将列出的项目从一个文件夹移动到另一个文件夹。

这是我将列出的项目移动到另一个文件夹的代码。

string sourcePath = @"C:\Users\Business\Music\Example\\";
            string targetPath = @"C:\Users\Business\Music\SENT\";

            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }

            string[] sourcefiles = Directory.GetFiles(listBox2.Items.ToString());

            foreach (string sourcefile in sourcefiles)
            {
                string fileName = Path.GetFileName(sourcefile);
                string destFile = Path.Combine(targetPath, fileName);

                File.Copy(sourcefile, destFile, true);
            }

标签: c#filelistbox

解决方案


推荐阅读