A Coder
A Coder

Reputation: 3046

The filename, directory name, or volume label syntax is incorrect, File.Copy

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.

Upvotes: 1

Views: 3221

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

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

Upvotes: 3

Related Questions