How can I use a file share and pass credentials?

K

kevin

I want to copy files from a file share which i have to pass credentials to.
Is there a way I can do this without mapping a drive in windows?

For example, I have serverA that I want to access in C# by \\serverA\mydir
and passing username/pass.

Thanks!
Kevin
(e-mail address removed)
 
W

Willy Denoyette [MVP]

You don't have to map a drive, but you need a network logon session with the
server.
To create such a session, you have a number of options:
1. You could create a logon session by calling LogonUser (Win32 api)
specifying the credentials of the user for the network logon session and
impersonate that user using the token obtained from LogonUser. Once this is
done you can copy files using the UNC path.
2 Or simply call "net use \\server\mydir password /user:xxxxx" command line
utility using System.Diagnostics.Process.Start.

Willy.
 
M

mikeb

kevin said:
I want to copy files from a file share which i have to pass credentials to.
Is there a way I can do this without mapping a drive in windows?

For example, I have serverA that I want to access in C# by \\serverA\mydir
and passing username/pass.

You can use P/Invoke to call the WNetAddConnection3() API. If you
specify an empty string as the lpLocalName member of the NETRESOURCE
structure parameter and pass in the right set of flags, the user will be
authenticated and no drive letter will be assigned to the share.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top