Click here to Skip to main content
15,897,518 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 629K   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

 
GeneralRe: Just using "Net Use"?? Pin
Nirosh29-Oct-06 23:32
professionalNirosh29-Oct-06 23:32 
Generalsimple solution for simpler requirements Pin
ren_reyes14-Nov-05 10:51
ren_reyes14-Nov-05 10:51 
GeneralRe: simple solution for simpler requirements Pin
dbarndt14-Mar-06 5:07
dbarndt14-Mar-06 5:07 
GeneralGreat article! Added getOpenDriveLetter. Pin
Member 354931016-Aug-05 3:03
Member 354931016-Aug-05 3:03 
GeneralRe: Great article! Added getOpenDriveLetter. Pin
aejw17-Aug-05 11:58
aejw17-Aug-05 11:58 
GeneralRe: Great article! Added getOpenDriveLetter. Pin
Anonymous19-Aug-05 11:05
Anonymous19-Aug-05 11:05 
GeneralRe: Great article! Added getOpenDriveLetter. Pin
Dewy6821-Aug-05 13:19
Dewy6821-Aug-05 13:19 
GeneralDisconnect bug Pin
jsantos9825-Jul-05 4:30
jsantos9825-Jul-05 4:30 
Hello there!

First of all, thanks for the article. It solved one big problem I was facing.

Now, it's my time to contribute with it...

According to Miscrosoft, we can have connection without the localname. You connection code works just fine with it, however, it fails on the disconnect. According to the documentation at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wnet/wnet/wnetcancelconnection2.asp,
we can replace the localname with the path connected.

I have changed it and this is working just fine with me. I'm posting it

private void zUnMapDrive(bool pfForce)
{
//call unmap and return
int iFlags=0;
if(lf_Persistent){iFlags+=CONNECT_UPDATE_PROFILE;}
int i = WNetCancelConnection2A(ls_Drive, iFlags, Convert.ToInt32(pfForce));
if (i != 0)
i = WNetCancelConnection2A(ls_ShareName, iFlags, Convert.ToInt32(pfForce));
if(i>0){throw new System.ComponentModel.Win32Exception(i);}
}

Hope this helps someone.

Regards,

Joao
GeneralRe: Disconnect bug Pin
aejw25-Jul-05 7:18
aejw25-Jul-05 7:18 
GeneralRe: Disconnect bug Pin
Anonymous25-Jul-05 7:20
Anonymous25-Jul-05 7:20 
GeneralRe: Disconnect bug Pin
AlegreBonifacio14-Jul-06 1:17
AlegreBonifacio14-Jul-06 1:17 
GeneralRe: Disconnect bug Pin
AlegreBonifacio14-Jul-06 1:26
AlegreBonifacio14-Jul-06 1:26 
GeneralRe: Disconnect bug Pin
Cybergenius15-Mar-07 3:40
Cybergenius15-Mar-07 3:40 
GeneralGreat Article Pin
Offlinesurfer19-Dec-04 10:36
Offlinesurfer19-Dec-04 10:36 
GeneralRe: Great Article Pin
lachlann20-Jun-05 9:19
lachlann20-Jun-05 9:19 
GeneralRe: Great Article Pin
aejw17-Aug-05 11:55
aejw17-Aug-05 11:55 
GeneralIPC$ Connections Pin
Member 88105830-May-04 2:32
Member 88105830-May-04 2:32 
GeneralRe: IPC$ Connections Pin
Member 8810581-Jun-04 11:53
Member 8810581-Jun-04 11:53 
GeneralRe: IPC$ Connections Pin
hle9920-Nov-06 14:22
hle9920-Nov-06 14:22 
QuestionArticle? Pin
Charlie Williams25-Apr-04 14:34
Charlie Williams25-Apr-04 14:34 
AnswerRe: Article? Pin
Huisheng Chen25-Apr-04 15:03
Huisheng Chen25-Apr-04 15:03 
GeneralRe: Article? Pin
Charlie Williams25-Apr-04 16:11
Charlie Williams25-Apr-04 16:11 
AnswerRe: Article? Pin
Michael Coyle28-Apr-04 10:10
Michael Coyle28-Apr-04 10:10 
AnswerRe: Article? Pin
brendo1120-May-04 4:00
brendo1120-May-04 4:00 
GeneralRe: Article? Pin
Charlie Williams20-May-04 11:43
Charlie Williams20-May-04 11:43 

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.