6

My application needs to access files on a remote machine that requires a username and password for accessing it.

I'm trying to find out if a directory exists (using Directory.Exists) to verify I can make the 'connection.

Is there a way to supply the username and password when working with remote directories? Currently Exists returns false.

Cheers,

adamwtiko
  • 2,765
  • 7
  • 38
  • 46

3 Answers3

5

Unfortunately not. You will need to wrap your code using extra code to handle impersonation of a user that does have access.

This article explains how to do it in code further down the page.

starskythehutch
  • 3,288
  • 1
  • 24
  • 35
2

Check out this thread on MSDN Forums.

It has code sample for using impersonation, and also explains a bit about using NetUseAdd in order to gain access if the machines are on different domains or non domains.

Mikael Svenson
  • 38,031
  • 6
  • 71
  • 73
1

Use the LogonUser api to login as a specific user and get a token and then use that token with WindowsIdentity.Impersonate.

Link with a sample. (do read the whole post to see it's security problems etc though).

Hans Olsson
  • 53,038
  • 14
  • 91
  • 113