I'm trying to copy a file from network shared folder to another folder in the same network. It threws the exception.

Verified and the file exists.

Source: \\servername\folder1\Old\ABC_1382.pdf

Destination: \\servername\folder1\New\

File.Copy(sourceFilePath, destiFilePath, true);

The file size is 400Kb.

1 Answers

3
Dmitry Bychenko On Best Solutions

Both Source and Destination should be file names. If destiFilePath is in fact a directory, let's add source file name (ABC_1382.pdf)

  string sourceFilePath = @"\\servername\folder1\Old\ABC_1382.pdf";

  string destiFilePath = @"\\servername\folder1\New\";

  // Uncomment, if you are not sure that directory exists 
  // and you want to create it with all subdirs
  // Directory.CreateDirectory(destiFilePath);

  File.Copy(sourceFilePath,
            Directory.Exists(destiFilePath) 
              ? Path.Combine(destiFilePath, Path.GetFileName(sourceFilePath))
              : destiFilePath,
            true);