Based on File.Copy Method , The second parameter is the new file name not the directory:
The name of the destination file. This cannot be a directory or an existing file.
You need to use a similar logic:
FileCopy(filestocopy[p],targetDir + "\\" + Path.GetFileName(filestocopy[p]));
Also it is recommended to check if the file already exists in the directory:
if (!File.Exists(targetDir + "\\" + Path.GetFileName(filestocopy[p])))
FileCopy(filestocopy[p],targetDir + "\\" + Path.GetFileName(filestocopy[p]));
If you need to overwrite any existing file you can add a boolean parameter :
FileCopy(filestocopy[p],targetDir + "\\" + Path.GetFileName(filestocopy[p]),true);
You need to specify the [file directory] + [File name] + [File extension] for the target parameter
So do something like this:
string destination = Path.Combine(targetDir, Path.GetFileName(filestocopy[p]));
File.Copy(filestocopy[p], destination);
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.