WNetUseConnection (mpr)
Last changed: -195.140.123.27

.
Summary
Makes a connection to a network resource. The function can redirect a local device to a network resource.

C# Signature:

[DllImport("Mpr.dll")]
private static extern int WNetUseConnection
(
    IntPtr      hwndOwner,
    NETRESOURCE lpNetResource,
    string      lpPassword,
    string      lpUserID,
    Connect     dwFlags,
    string      lpAccessName,
    string      lpBufferSize,
    string      lpResult
);

VB Signature:

Declare Function WNetUseConnection Lib "mpr.dll" (TODO) As TODO

User-Defined Types:

public enum ResourceScope
{
    CONNECTED   = 0x00000001,
    GLOBALNET   = 0x00000002,
    REMEMBERED  = 0x00000003,
}

public enum ResourceType
{
    ANY     = 0x00000000,
    DISK    = 0x00000001,
    PRINT   = 0x00000002,
}

public enum ResourceDisplayType
{
    GENERIC      = 0x00000000,
    DOMAIN       = 0x00000001,
    SERVER       = 0x00000002,
    SHARE        = 0x00000003,
    FILE         = 0x00000004,
    GROUP        = 0x00000005,
    NETWORK      = 0x00000006,
    ROOT         = 0x00000007,
    SHAREADMIN   = 0x00000008,
    DIRECTORY    = 0x00000009,
    TREE         = 0x0000000A,
    NDSCONTAINER = 0x0000000A,
}

[Flags]
public enum ResourceUsage
{
    CONNECTABLE    = 0x00000001,
    CONTAINER      = 0x00000002,
    NOLOCALDEVICE  = 0x00000004,
    SIBLING        = 0x00000008,
    ATTACHED       = 0x00000010,
}

[Flags]
public enum Connect
{
    UPDATE_PROFILE  = 0x00000001,
    INTERACTIVE     = 0x00000008,
    PROMPT          = 0x00000010,
    REDIRECT        = 0x00000080,
    LOCALDRIVE      = 0x00000100,
    COMMANDLINE     = 0x00000800,
    CMD_SAVECRED    = 0x00001000,
}

[StructLayout(LayoutKind.Sequential)]
private class NETRESOURCE
{
    public ResourceScope        dwScope       = 0;
    public ResourceType         dwType        = 0;
    public ResourceDisplayType  dwDisplayType = 0;
    public ResourceUsage        dwUsage       = 0;

    public string lpLocalName  = "";
    public string lpRemoteName = "";
    public string lpComment    = "";
    public string lpProvider   = "";
}

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

public static void UseConnection(string localName, string remoteName, string username, string password)
{
    NETRESOURCE nr = new NETRESOURCE()
    {
       dwType       = ResourceType.DISK,
       lpLocalName  = localName,
       lpRemoteName = remoteName,
    };

    ThrowIfError(WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null));
}

public static void UseConnection(string localName, string remoteName, bool promptUser = false)
{
    NETRESOURCE nr = new NETRESOURCE()
    {
       dwType       = ResourceType.DISK,
       lpLocalName  = localName,
       lpRemoteName = remoteName,
    };

    ThrowIfError(WNetUseConnection(IntPtr.Zero, nr, null, null, promptUser ? Connect.INTERACTIVE | Connect.PROMPT : 0, null, null, null));
}

[DebuggerStepThrough]
internal static void ThrowIfError(int win32ErrorCode)
{
    if (win32ErrorCode != 0)
    {
       throw new Win32Exception(win32ErrorCode);
    }
}

Documentation