Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
NetUseAdd (netapi32)
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. Connections added by NetUseAdd are not shown in the Explorer. You should use one of the WNetAddConnection methods to make the networkdrive visible in the explorer.

C# Signature:

using BOOL = System.Boolean;
using DWORD = System.UInt32;
using LPWSTR = System.String;
using NET_API_STATUS = System.UInt32;

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

User-Defined Types:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct USE_INFO_2
     internal LPWSTR ui2_local;
     internal LPWSTR ui2_remote;
     internal LPWSTR ui2_password;
     internal DWORD  ui2_status;
     internal DWORD  ui2_asg_type;
     internal DWORD  ui2_refcount;
     internal DWORD  ui2_usecount;
     internal LPWSTR ui2_username;
     internal LPWSTR ui2_domainname;


These structures are declared using C# ability to alias types with the using statement.

Tips & Tricks:

You need to declare the Struct in a separate code file called Structures, and then reference it in your main code instance. Otherwise the code will not work.

Also, you will need to add the following using statements to it:

using System.Runtime.InteropServices;

using BOOL = System.Boolean;

using DWORD = System.UInt32;

using LPWSTR = System.String;

using NET_API_STATUS = System.UInt32;

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:


NetUseAdd on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version