NetUseAdd (netapi32)
Last changed: Christian Rodemeyer-84.152.7.204

.
Summary
The NetUseAdd function establishes a connection between the local computer and a remote server. You can specify a local drive letter or a printer device to connect. If you do not specify a local drive letter or printer device, the function authenticates the client with the server for future connections.

You can also use the WNetAddConnection2 and WNetAddConnection3 functions to redirect a local device to a network resource.

C# Signature:

[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern NET_API_STATUS NetUseAdd(
     LPWSTR UncServerName,
     DWORD Level,
     ref USE_INFO_2 Buf,
     out DWORD ParmError);

User-Defined Types:

USE_INFO_2

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

USE_INFO_2 useInfo    = new USE_INFO_2();
useInfo.ui2_local     = "E:";
useInfo.ui2_remote    = @"\\machine\share";
useInfo.ui2_password  = "password";
useInfo.ui2_asg_type  = 0;    //disk drive
useInfo.ui2_usecount  = 1;
useInfo.ui2_username  = "user";
useInfo.ui2_domainname= "domain";

uint paramErrorIndex;
uint returnCode = NetUseAdd(null, 2, ref useInfo, out paramErrorIndex);
if (returnCode != 0)
{
      throw new Win32Exception((int)returnCode);
}

Alternative Managed API:

TODO

Documentation
NetUseAdd on MSDN