Click here to Skip to main content
Click here to Skip to main content

Map Network Drive (API)

By , 18 May 2004
 

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, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow Do I determine whether the specified drive is currently connected or not [modified]memberPrashant Bangaluru8 May '13 - 3:29 
Questionpublished version?memberfanff30001 Jan '13 - 18:05 
QuestionYour help neededmemberSteve Ween2 Nov '12 - 13:17 
Questionaejw networkmembertari_mrv1 Oct '12 - 2:18 
QuestionAsking for credentials even if I supply them in Windows 7.memberMike Miller9 Aug '12 - 12:27 
QuestionLatest versionmemberjeremycafe111129 Apr '12 - 3:09 
QuestionUnused drive lettermemberRoss Chang31 Oct '11 - 18:47 
QuestionSitemembernenadvuckovic24 Oct '11 - 3:48 
Questionfor vb6memberafdoal18 Sep '11 - 1:30 
Questiondoes not work under win7 limited accountmemberxRayen13 Sep '11 - 4:13 
QuestionError in using Map network drive APImemberk.parvez31 Mar '11 - 21:14 
GeneralMy vote of 5memberJulijan Sribar17 Jan '11 - 23:02 
GeneralMy vote of 5memberemirucan10 Jan '11 - 0:15 
GeneralThank you!!!memberGuillermo Toro8 Dec '10 - 13:17 
GeneralLast versionmemberMember 60274910 Feb '10 - 6:11 
GeneralRe: Last versionmemberFaxedHead27 Feb '10 - 21:45 
GeneralRe: Last versionmemberMember 6027491 Mar '10 - 7:40 
GeneralNew Version 0028memberTim Gladney5 Feb '10 - 3:42 
GeneralthankssssssssmemberMember 28813132 Feb '10 - 0:01 
QuestionHow do you expand a mapped drive?memberSparkyNZ26 Nov '09 - 8:30 
GeneralThis is an old versionmemberyrodrigu4 Nov '09 - 15:33 
GeneralLPT bugmemberdmageiras11 Jun '09 - 1:09 
GeneralThank youmemberJosh Andy27 May '09 - 21:16 
GeneralNew patch makes it doesn't work any more on Windows 2003membersongkai12 May '09 - 23:50 
GeneralServer OS 2003memberMember 456327911 May '09 - 11:00 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 19 May 2004
Article Copyright 2004 by aejw
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid