Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Map Network Drive (API)

0.00/5 (No votes)
18 May 2004 1  
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.
    using aejw.Network;
  • Example (Mapping a network drive)

     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)

     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.
     cNetworkDrive oNetDrive = new cNetworkDrive();
  • Mapping a network drive

    • //Map drive with current user credentials
      
      oNetDrive.LocalDrive = "m:";
      oNetDrive.ShareName = "\\ComputerName\Share1"
      oNetDrive.MapDrive();
    • //Map drive with specified user credentials
      
      oNetDrive.LocalDrive = "m:";
      oNetDrive.ShareName = "\\ComputerName\Share1"
      oNetDrive.MapDrive("Bob_Username","Bob_Password");
    • //Map drive with and prompt user for credentials
      
      oNetDrive.LocalDrive = "m:";
      oNetDrive.ShareName = "\\ComputerName\Share1"
      oNetDrive.PromptForCredentials = true;
      oNetDrive.MapDrive();
    • //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

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

    • //Display windows connection dialog
      
      oNetDrive.ShowConnectDialog(this);
      //Display windows disconnection dialog
      
      oNetDrive.ShowDisconnectDialog(this);
    • //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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here