fatin53
fatin53

Reputation: 91

copy multiple files from listbox to specified folder

there are in the files of my listbox ı want copy files these specified path for example c:\ or any path but error be (value cannot be null parameter name path) error how ı can copy specified path ı wirte this code

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

Upvotes: 0

Views: 1559

Answers (1)

Roberto Conte Rosito
Roberto Conte Rosito

Reputation: 2098

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

Upvotes: 1

Related Questions