0

I'm writing a program that, among other things, needs to copy a particular file to a network folder. Since I'm on a company network, the credentials needed to access that folder are the same as my Windows credentials.

The program works if I open the folder in Explorer, provide my username and password, and then run the uploader. It doesn't work without first providing that username and password.

How do I tell System.IO to supply my DefaultNetworkCredentials to the Copy method? Or is there another method I can use to get this done?

string pathToFile = "myfile.csv";
string pathToRemoteFile = "\\server.domain.tld\Documents\Subfolder\myfile.csv"

System.IO.File.Copy(pathToFile, pathToRemoteFile); // Fails with IOException "can't find network path"

Thanks!

~ Wogan

Wogan
  • 1,335
  • 12
  • 17
  • I believe there is a more universal way: http://stackoverflow.com/questions/295538/how-to-provide-user-name-and-password-when-connecting-to-a-network-share/39540451#39540451 – Pavel Kovalev Sep 16 '16 at 21:44

1 Answers1

2

The error suggests that it is an incorrect path rather than a permissions problem.

Try this:

string pathToRemoteFile = @"\\server.domain.tld\Documents\Subfolder\myfile.csv"

[The @ is the string literal quoting symbol; without it the backslash is a special character]

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • you can also escape all the '\' chars with '\\'. But Mitch'es solution is best. – Koekiebox Sep 09 '09 at 10:38
  • Sadly, that's not it - Like I said in my original question, the code works fine if I browse to and log in to the folder path in Explorer before I run the program. – Wogan Sep 09 '09 at 11:05
  • Oops, and that pseudocode sample wasn't copied from the program, it was just for demonstration purposes. – Wogan Sep 09 '09 at 11:05