4

If I open up a File Explorer, and type \\somesite.com\d$ I will get prompted for credentials, and after entering them I get access to the file system. How can I do this in C# to access \\somesite.com\d$ to just see the files/directories in code?

Isn't there something baked into .NET that does this? The solutions I've found on Google want to import different .dlls and do the plumbing themselves...

I've tried this:

  string username = "username";
  string password = "password";
  string domain = "domain";

  NetworkCredential theNetworkCredential = new NetworkCredential(username, password, domain);
  CredentialCache theNetCache = new CredentialCache();
  theNetCache.Add(new Uri(@"\\somesite.com"), "Basic", theNetworkCredential);
  string[] theFolders = System.IO.Directory.GetDirectories(@"\\somesite.com\d$");

But it says the username/password is incorrect. If I first go to the File Explorer and log in, and run the program again, it works fine.

pkr
  • 1,723
  • 5
  • 25
  • 43
  • There is nothing built-in to .NET to do this. :-( Searching for "C# map network drive" I see lots of solutions, mostly hacks, but none of them are pure C#. – Moby Disk Jan 28 '14 at 15:54
  • The way to do it (not pure .NET but actually works): http://stackoverflow.com/questions/2541504/how-do-i-pass-credentials-to-a-machine-so-i-can-use-microsoft-win32-registrykey – pkr May 05 '14 at 15:11

0 Answers0