Click here to Skip to main content
15,887,746 members
Articles / Programming Languages / C#
Article

Map Network Drive (API)

Rate me:
Please Sign up or sign in to vote.
4.64/5 (65 votes)
18 May 2004CC (ASA 2.5)1 min read 627.3K   21.2K   139   117
Class for interfacing to the "map network drive" windows interface

Image - netdrive0015.jpg

What does it do?

This is a class for interfacing with windows map network drive API's.

Introduction to the class...

  • Getting started

    Add the class file "cNetworkDrives0015.cs" to your project / solution.
    Add the "using" definition to your form, etc.
    C#
    using aejw.Network;
  • Example (Mapping a network drive)

    C#
    NetworkDrive oNetDrive = new aejw.Network.NetworkDrive();
    try{
       oNetDrive.LocalDrive = "m:";
       oNetDrive.ShareName = "\\ComputerName\Share"
       oNetDrive.MapDrive();
    }catch(Exception err){
       MessageBox.Show(this,"Error: "+err.Message);
    }
    oNetDrive = null;
    
  • Example (Unmapping a network drive)

    C#
    NetworkDrive oNetDrive = new aejw.Network.NetworkDrive();
    try{
       oNetDrive.LocalDrive = "m:";
       oNetDrive.UnMapDrive();
    }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.
C#
cNetworkDrive oNetDrive = new cNetworkDrive();
  • Mapping a network drive

    • C#
      //Map drive with current user credentials
      oNetDrive.LocalDrive = "m:";
      oNetDrive.ShareName = "\\ComputerName\Share1"
      oNetDrive.MapDrive();
    • C#
      //Map drive with specified user credentials
      oNetDrive.LocalDrive = "m:";
      oNetDrive.ShareName = "\\ComputerName\Share1"
      oNetDrive.MapDrive("Bob_Username","Bob_Password");
    • C#
      //Map drive with and prompt user for credentials
      oNetDrive.LocalDrive = "m:";
      oNetDrive.ShareName = "\\ComputerName\Share1"
      oNetDrive.PromptForCredentials = true;
      oNetDrive.MapDrive();
    • C#
      //Map drive using a persistent connection
      oNetDrive.LocalDrive = "m:";
      oNetDrive.Persistent = true;
      oNetDrive.SaveCredentials = true;
      oNetDrive.ShareName = "\\ComputerName\Share1"
      oNetDrive.MapDrive("Bob_Username","Bob_Password");
  • Unmapping a network drive

    • C#
      //Unmap a network connection
      oNetDrive.LocalDrive = "m:";
      oNetDrive.ShareName = "\\ComputerName\Share1"
      oNetDrive.UnMapDrive();
    • C#
      //Unmap a network connection ignoring network related errors
      oNetDrive.LocalDrive = "m:";
      oNetDrive.Force = true;
      oNetDrive.ShareName = "\\ComputerName\Share1"
      oNetDrive.UnMapDrive();
  • Other functions

    • C#
      //Display windows connection dialog
      oNetDrive.ShowConnectDialog(this);
      //Display windows disconnection dialog
      oNetDrive.ShowDisconnectDialog(this);
    • C#
      //Restore all persistent connections
      oNetDrive.RestoreDrives();

History

  • 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(...), etc...
    • Added Persistant option, Used for reconnecting 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 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

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
Web Developer
New Zealand New Zealand
C#, VB.net (Web and forms), SQL Server, MySQL, ASP, Win32 API, ...
Site: aejw.com

Comments and Discussions

 
GeneralMy vote of 5 Pin
emirucan10-Jan-11 0:15
emirucan10-Jan-11 0:15 
GeneralThank you!!! Pin
Guillermo Toro8-Dec-10 13:17
Guillermo Toro8-Dec-10 13:17 
GeneralLast version Pin
Member 60274910-Feb-10 6:11
Member 60274910-Feb-10 6:11 
GeneralRe: Last version Pin
FaxedHead27-Feb-10 21:45
FaxedHead27-Feb-10 21:45 
GeneralRe: Last version Pin
Member 6027491-Mar-10 7:40
Member 6027491-Mar-10 7:40 
GeneralNew Version 0028 Pin
Tim Gladney5-Feb-10 3:42
Tim Gladney5-Feb-10 3:42 
Generalthankssssssss Pin
Member 28813132-Feb-10 0:01
Member 28813132-Feb-10 0:01 
QuestionHow do you expand a mapped drive? Pin
SparkyNZ26-Nov-09 8:30
SparkyNZ26-Nov-09 8:30 
GeneralThis is an old version Pin
Yvan Rodrigues4-Nov-09 15:33
professionalYvan Rodrigues4-Nov-09 15:33 
GeneralLPT bug Pin
dmageiras11-Jun-09 1:09
dmageiras11-Jun-09 1:09 
GeneralThank you Pin
Rami Shareef27-May-09 21:16
Rami Shareef27-May-09 21:16 
GeneralNew patch makes it doesn't work any more on Windows 2003 Pin
Song Kai12-May-09 23:50
Song Kai12-May-09 23:50 
GeneralServer OS 2003 Pin
Member 456327911-May-09 11:00
Member 456327911-May-09 11:00 
GeneralYour website is down AEJW.com Pin
mm7777-May-09 22:20
mm7777-May-09 22:20 
GeneralServer O/S Pin
mbaocha4-May-09 16:14
mbaocha4-May-09 16:14 
GeneralAttempt to access invalid address Pin
Member 365220314-Dec-08 19:21
Member 365220314-Dec-08 19:21 
GeneralRe: Attempt to access invalid address Pin
aejw14-Dec-08 20:19
aejw14-Dec-08 20:19 
GeneralRe: Attempt to access invalid address Pin
Member 365220315-Dec-08 6:01
Member 365220315-Dec-08 6:01 
QuestionDo it once or everytime? What is the latest version? Pin
King-----Kong27-Nov-08 19:39
King-----Kong27-Nov-08 19:39 
AnswerRe: Do it once or everytime? What is the latest version? Pin
aejw14-Dec-08 19:59
aejw14-Dec-08 19:59 
GeneralVista : Access denied Pin
Pierre Liétar12-Nov-08 0:08
Pierre Liétar12-Nov-08 0:08 
GeneralRe: Vista : Access denied Pin
aejw13-Nov-08 11:13
aejw13-Nov-08 11:13 
Questionproblem using in an aspx button Pin
raziel782-Oct-08 0:58
raziel782-Oct-08 0:58 
QuestionWorks on Windows XP Home? Pin
Manuel Quelhas10-Jul-08 2:51
Manuel Quelhas10-Jul-08 2:51 
AnswerRe: Works on Windows XP Home? Pin
aejw20-Jul-08 14:58
aejw20-Jul-08 14:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.