简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM