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,865
  • 8
  • 39
  • 47

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,328
  • 1
  • 25
  • 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
  • 39,181
  • 7
  • 73
  • 79
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).

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116