0

在我的列表框的文件中有我想要复制文件这些指定的路径,例如 c:\ 或任何路径,但错误是(值不能是空参数名称路径)错误如何复制指定的路径我写这个代码

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

1 回答 1

1

I think the problem is here:

string destinationfolder; 

You declare an empty string and after try to get DirectoryInfo from what? And Empty string? This thrown an Exception. You can see your code like this:

DirectoryInfo di = new DirectoryInfo(""); 

This code throw always an Exception. The question is: what you need in "destinationFolder" parameter?

This is a sample file copy:

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);
}
于 2012-04-18T10:37:32.093 回答