Click here to Skip to main content
15,888,527 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.5K   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: Reconnect Drives Pin
aejw20-Aug-06 21:13
aejw20-Aug-06 21:13 
GeneralstNetRes.sComment Pin
RasmusFoged31-Jul-06 8:45
RasmusFoged31-Jul-06 8:45 
QuestionWindows 2003: No network provider accepted the given network path. Pin
markoo25-Jul-06 1:43
markoo25-Jul-06 1:43 
AnswerRe: Windows 2003: No network provider accepted the given network path. Pin
aejw26-Jul-06 20:06
aejw26-Jul-06 20:06 
GeneralRe: Windows 2003: No network provider accepted the given network path. Pin
Great George Smith2-Oct-07 21:36
Great George Smith2-Oct-07 21:36 
AnswerRe: Windows 2003: No network provider accepted the given network path. Pin
lwgoh21-May-07 19:48
lwgoh21-May-07 19:48 
GeneralUsed your code using my web application Pin
dabuskol16-Jul-06 19:58
dabuskol16-Jul-06 19:58 
GeneralRe: Used your code using my web application Pin
aejw16-Jul-06 23:32
aejw16-Jul-06 23:32 
This is rather complex... you are getting the error message because the application is isolated as such, meaning because of the security on the .net web server your web application does not have the security rights to allow you to map a network drive. I would suggest you modify the assembly / user access rights on your web server under the “Control Panel\Administrative Tools\Microsoft .NET Framework x.x Configuration” to allow the assembly full access. If you continue to have issues, notify me and ill try and hunt down another solution.



------------------------------------------------------------------------------------
aejw (http://www.aejw.com/)
GeneralRe: Used your code using my web application Pin
dabuskol18-Jul-06 19:29
dabuskol18-Jul-06 19:29 
GeneralRe: Used your code using my web application Pin
aejw18-Jul-06 20:42
aejw18-Jul-06 20:42 
GeneralRe: Used your code using my web application Pin
dabuskol19-Jul-06 1:08
dabuskol19-Jul-06 1:08 
GeneralWanted to thank you. Pin
Slackshot29-Jun-06 4:34
Slackshot29-Jun-06 4:34 
Questionunmapping only disconnects?? Pin
oscar scott1-Mar-06 3:26
oscar scott1-Mar-06 3:26 
AnswerRe: unmapping only disconnects?? Pin
aejw30-Apr-06 19:00
aejw30-Apr-06 19:00 
GeneralUsing Windows Service Pin
lakhanisa28-Feb-06 23:11
lakhanisa28-Feb-06 23:11 
GeneralRe: Using Windows Service Pin
aejw15-May-06 18:30
aejw15-May-06 18:30 
GeneralRe: Using Windows Service Pin
lakhanisa17-May-06 8:14
lakhanisa17-May-06 8:14 
GeneralRe: Using Windows Service Pin
NPayette17-May-06 9:42
NPayette17-May-06 9:42 
GeneralRe: Using Windows Service Pin
aejw18-May-06 0:28
aejw18-May-06 0:28 
GeneralRe: Using Windows Service Pin
alaiasRogue28-Aug-06 3:40
alaiasRogue28-Aug-06 3:40 
GeneralSame class with different OS Pin
lakhanisa21-Feb-06 21:23
lakhanisa21-Feb-06 21:23 
GeneralRe: Same class with different OS Pin
aejw15-May-06 18:36
aejw15-May-06 18:36 
GeneralRe: Same class with different OS [modified] Pin
cyberninja6624-Jan-07 21:26
cyberninja6624-Jan-07 21:26 
Generalgetting all the drive names Pin
oscar scott20-Feb-06 7:49
oscar scott20-Feb-06 7:49 
GeneralRe: getting all the drive names Pin
Slackshot29-Jun-06 4:31
Slackshot29-Jun-06 4:31 

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.