views:

26

answers:

3

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,

+1  A: 

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
+1 excellent link.
slugster
A: 

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).

ho1
+1  A: 

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