C# Map Network Drive (API)

What It Does

This is a class for interfacing with the Windows Map Network Drive APIs.


This class was originally written to aid me during a contract for writing a startup automation application on Windows 2000. Since then, it has been used or tested with Windows XP, XP64 2003, and Vista beta2. The class was structured around some C++ MSDN documents/examples of the network API. Although there are plenty of other methods for mapping a network drive, I chose this method because it gives me the ability to report errors back to the application and provides access to the various network options.

Introduction to the Class

Getting started

Add the class file "cNetworkDrives0015.cs" to your project/solution. Add the "using" definition to your form, and so forth.

using aejw.Network;

Example: Mapping a network drive

NetworkDrive oNetDrive  = new aejw.Network.NetworkDrive();
   oNetDrive.LocalDrive = "m:";
   oNetDrive.ShareName  = "\\ComputerName\Share"
}catch(Exception err){
   MessageBox.Show(this,"Error: "+err.Message);
oNetDrive = null;

Example: Unmapping a network drive

NetworkDrive oNetDrive = new aejw.Network.NetworkDrive();
   oNetDrive.LocalDrive = "m:";
}catch(Exception err){
   MessageBox.Show(this,"Error: "+err.Message);
oNetDrive = null;

Username and Password Functions

The following examples require the object/class to be declared.

cNetworkDrive oNetDrive = new cNetworkDrive();

Mapping a network drive

  • Map drive with current user credentials
    oNetDrive.LocalDrive = "m:";
    oNetDrive.ShareName  = "\\ComputerName\Share1"
  • Map drive with specified user credentials
    oNetDrive.LocalDrive = "m:";
    oNetDrive.ShareName  = "\\ComputerName\Share1"
  • Map drive with and prompt user for credentials
    oNetDrive.LocalDrive = "m:";
    oNetDrive.ShareName  = "\\ComputerName\Share1"
    oNetDrive.PromptForCredentials = true;
  • Map drive using a persistent connection
    oNetDrive.LocalDrive = "m:";
    oNetDrive.Persistent = true;
    oNetDrive.SaveCredentials = true;
    oNetDrive.ShareName = "\\ComputerName\Share1"

Unmapping a network drive

  • Unmap a network connection
    oNetDrive.LocalDrive = "m:";
    oNetDrive.ShareName  = "\\ComputerName\Share1"
  • Unmap a network connection ignoring network related errors
    oNetDrive.LocalDrive = "m:";
    oNetDrive.Force      = true;
    oNetDrive.ShareName  = "\\ComputerName\Share1"

Other functions

  • Display windows connection dialog
    //Display windows disconnection dialog
  • Restore all persistent connections


10th August 2006: build0017

  • Corrected persistent bug in demo application when unmapping drive.

11th May 2006: build0017

  • Cleaned and optimised code.

14th May 2004: build0015

  • LocalDrive and ShareName are now properties.
  • Dialog functions now use a form object instead of a window handle.
  • Renaming scheme for public functions and properties, MapNetworkDrive(...) is now MapDrive(...), and so on.
  • Added Persistant option. Used to reconnect a drive at logon.
  • Added SaveCredentials option. Allows windows to remember the user credentials when reconnecting a persistent connection.
  • Added Force option, for MapDrive calls. If a drive is connected, it will disconnect that drive and then reconnect to the new share.
  • Added PromptForCredintals option, for MapDrive calls. Windows will ask for a username and password to use with the connection.
  • Added RestoreDrives function that restores persistent connections.

30th April 2004: build0012

  • Code refinements and tidying, added comments to the class.

27th April 2004: build0011

  • Adjusted declare tags, tidied class and article.

26th April 2004: build0010

  • First version posted online.

About the Author

Adam Woods

Adam is a .net contractor. (C#, VB.net, ASP.net, Win32 API, My SQL, SQL Server, etc etc...) Visit aejw.com for details...



  • Successfully mapped in Windows 7 OS system. But failed in Windows 8 OS system.

    Posted by Thanuja George on 12/22/2014 03:05am

    This code helped me to map virtual drive in windows 7 OS system and it is available in 'NetWork Location' (under Computer folder). In Windows 8 OS system the drive is mapped successfully, but the drive is not visible in 'NetWork Location'(under Computer folder).

  • Visual Studio 2008

    Posted by Ed on 09/23/2014 11:58am

    Hi, I cannot open the .sln or .csproj file in Visual Studio 2008, any suggestions. Thanks.

  • Map Network Drive to online server

    Posted by David on 09/06/2014 11:40am

    I tried to use this app to map a network drive to an online server e.g let www.abcxyz.co.uk map to online address ftp.abcxyz.co.uk : I can do this manually of course through windows explorer but I want to do it via a program. Any ideas?

  • Developer

    Posted by Todd D on 06/11/2014 08:22am

    Was hopeful that it would also provide the UNC for a mapped drive. But thanks for what it does.

  • Thanks

    Posted by Hamzah Nashwan on 06/03/2014 02:05am

    Thanks it worked. I have been stuck in this point.

  • Thanks, it helps me ;)

    Posted by Samir on 12/24/2013 04:41am


  • han

    Posted by han on 07/05/2012 02:54am

    it can use HTTPS??

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • On-demand Event Event Date: September 23, 2015 The cloud is not just about a runtime platform for your projects – now, you can do your development in the cloud, too. Check out this webcast to learn how the cloud improves your development experience and team collaboration. Join Dana Singleterry, Principal Product Manager for Oracle Dev Tools, as he discusses how to simplify every aspect of the development lifecycle, including requirements gathering, version management, code reviews, build automation, and …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date