How can I use a file share and pass credentials?

  • Thread starter Thread starter kevin
  • Start date Start date
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)
 
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.
 
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.
 
Back
Top