I want to copy a specific file from one location to another. I have a Message Box that prints where the file is copied source and destination as per below:
I get the below error:
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);