Click here to Skip to main content
15,881,089 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 626.7K   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

 
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 
QuestionHow do you install program Pin
chris guajardo18-Jun-08 10:50
chris guajardo18-Jun-08 10:50 
AnswerRe: How do you install program [modified] Pin
aejw19-Jun-08 11:59
aejw19-Jun-08 11:59 
QuestionMultiple connections Exception Pin
NickKa21-May-08 10:32
NickKa21-May-08 10:32 
AnswerRe: Multiple connections Exception Pin
aejw19-Jun-08 23:38
aejw19-Jun-08 23:38 
GeneralRe: Multiple connections Exception Pin
Rami Shareef31-May-09 23:15
Rami Shareef31-May-09 23:15 
To gain one more mapped drive, connect it to the IP of the server not the name
and to gain another one too, give your DNS an alias and connect throught the alias not the server name

Check this
http://support.microsoft.com/kb/173199/[^]

Hope this help Smile | :)
GeneralMap netdriver error. [modified] Pin
Paul Zhang16-Mar-08 16:24
Paul Zhang16-Mar-08 16:24 
GeneralRe: Map netdriver error. Pin
aejw17-Mar-08 11:52
aejw17-Mar-08 11:52 
GeneralRe: http webdrive (webdav) possible ? Pin
Matterz11-Nov-07 1:09
Matterz11-Nov-07 1:09 
GeneralRe: http webdrive (webdav) possible ? Pin
aejw11-Nov-07 5:40
aejw11-Nov-07 5:40 
GeneralRe: http webdrive (webdav) possible ? Pin
Matterz26-Jan-08 17:31
Matterz26-Jan-08 17:31 
Questionhttp webdrive (webdav) possible ? Pin
Matterz10-Nov-07 11:04
Matterz10-Nov-07 11:04 
AnswerRe: http webdrive (webdav) possible ? Pin
aejw10-Nov-07 17:54
aejw10-Nov-07 17:54 
GeneralUnmapping Error Pin
Sturges9-Nov-07 5:12
Sturges9-Nov-07 5:12 
GeneralRe: Unmapping Error Pin
aejw10-Nov-07 18:00
aejw10-Nov-07 18:00 
GeneralRe: Unmapping Error Pin
Sturges13-Nov-07 8:19
Sturges13-Nov-07 8:19 
GeneralThank you Pin
André Stroebel28-Sep-07 2:36
André Stroebel28-Sep-07 2:36 
GeneralUnMapDrive [modified] Pin
Daniel B.18-Jun-07 5:55
Daniel B.18-Jun-07 5:55 
GeneralRe: UnMapDrive Pin
aejw20-Jun-07 18:36
aejw20-Jun-07 18:36 
GeneralThanks Pin
Sascha66613-Dec-06 5:44
Sascha66613-Dec-06 5:44 
GeneralFor Update.... Pin
Sixcode29-Oct-06 2:47
Sixcode29-Oct-06 2:47 
GeneralWorking with VB.NET [modified] Pin
dietercools10-Sep-06 12:58
dietercools10-Sep-06 12:58 
GeneralReconnect Drives Pin
Jacob Korsgaard19-Aug-06 0:14
Jacob Korsgaard19-Aug-06 0:14 

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.