3

I am trying to list files on a UNC folder

[HttpGet]
public IEnumerable<string> Get()
{            
    var list = Directory.GetFiles(@"\\files.mydomain.com\folder");

    return list;
}

But it is giving this Exception

System.UnauthorizedAccessException: 'Access to the path '\\files.mydomain.com\folder' is denied.'

How can I inform the username and password using C# code on ASP.Net Core 2?

Athari
  • 33,702
  • 16
  • 105
  • 146
Tony
  • 16,527
  • 15
  • 80
  • 134
  • I tested this, it works on my Windows 10 PC https://stackoverflow.com/a/1435789/194717 – Tony Oct 11 '17 at 19:13

1 Answers1

-5

Try this.

NetworkCredential theNetworkCredential = new NetworkCredential(username, password, domain);

CredentialCache theNetCache = new CredentialCache();

theNetCache.Add(@"\\files.mydomain.com", theNetworkCredential, "Basic", theNetworkCredential);

Then do whatever, such as getting a list of folders:

string[] theFiles = System.IO.Directory.GetFiles(@"\\files.mydomain.com\folder");
  • Welcome to SO ! code-only answers might be considered as low quality and hence removed. Can you please elaborate on how this answers the question ? – Gilles Gouaillardet Mar 26 '18 at 01:17
  • I tried many many variations of those code and couldn't get it to work to access a remote share using a "local" user on the remote server. i.e. trying to access `\\fileserver\share` with a username of the format `fileserver\localUsername` – ec2011 May 04 '18 at 15:03
  • I wanted to create a System.IO.FileStream with this and that might be why it didn't work for. But if you're trying to use a WebRequest this method might still work - e.g. https://stackoverflow.com/a/33377015/990262 – ec2011 May 04 '18 at 15:52