将多个文件从列表框复制到指定的文件夹

时间:2012-04-18 10:18:23

标签: c# listbox

在我的列表框的文件中有想要复制文件这些指定的路径,例如c:\或任何路径,但错误是(值不能为空参数名称路径)错误如何ı可以复制指定的路径ıwirte此代码< / p>

string source, fileToCopy, target;
string sourcefolder1;
string destinationfolder;
DirectoryInfo di = new DirectoryInfo(destinationfolder);
FileInfo[] annfiles;

foreach (string s in listBox1.Items)
{
     fileToCopy = s;
     source = Path.Combine(sourcefolder1, fileToCopy);
     target = Path.Combine(destinationfolder, fileToCopy);
     File.Copy(source, target);

     annFiles = di.GetFiles();
}

1 个答案:

答案 0 :(得分:1)

我认为问题在于:

string destinationfolder; 

你声明一个空字符串,并尝试从什么获取DirectoryInfo?还有空字符串?抛出异常。你可以看到你的代码:

DirectoryInfo di = new DirectoryInfo(""); 

此代码始终为异常。 问题是:“destinationFolder”参数需要什么?

这是一个示例文件副本:

string sourceFolder = @"C:\Documents";
string destinationFolder = "@"C:\MyDocumentsCopy";

DirectoryInfo directory = new DirectoryInfo(sourceFolder);
FileInfo[] files = directory.GetFiles();
foreach(var file in files)
{
    string destinationPath = Path.Combine(destinationFolder, file.Name);
    File.Copy(file.Fullname, destinationPath);
}