Click here to Skip to main content
Licence CC (ASA 2.5)
First Posted 24 Apr 2004
Views 308,606
Downloads 5,683
Bookmarked 100 times

Map Network Drive (API)

By aejw | 18 May 2004
Class for interfacing to the "map network drive" windows interface
4 votes, 7.5%
1

2
3 votes, 5.7%
3
10 votes, 18.9%
4
36 votes, 67.9%
5
4.65/5 - 53 votes
4 removed
μ 4.38, σa 1.98 [?]

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralServer O/S Pinmembermbaocha17:14 4 May '09  
GeneralAttempt to access invalid address PinmemberMember 365220320:21 14 Dec '08  
GeneralRe: Attempt to access invalid address Pinmemberaejw21:19 14 Dec '08  
GeneralRe: Attempt to access invalid address PinmemberMember 36522037:01 15 Dec '08  
QuestionDo it once or everytime? What is the latest version? PinmemberKing-----Kong20:39 27 Nov '08  
AnswerRe: Do it once or everytime? What is the latest version? Pinmemberaejw20:59 14 Dec '08  
GeneralVista : Access denied PinmemberPierre Liétar1:08 12 Nov '08  
Hi,
 
I tried the code using XP, it works great!
I tried it using Vista to map a server shared folder, it works great!
I tried it using Vista to map a network HD with a public shared folder, it fails (Access denied).
I so tried to check "ask credentials". I fullfil the credentials when asked. I works!
I then tried to uncheck "ask credentials" and give credentials in the form. It failed...
 
Does anyone have an idea of what's happening?
 
I tried using the windows vista interface to make my network drive. There's no problem.
I tried to make it using the "net use" function, the same problem as the code provided occured : "access denied" (Note : the same command works great on XP)
 
The network HD is a linux based interface. Does it change anything?
 
Any help would be really appreciate.
 
-----
Pierre Liétar

GeneralRe: Vista : Access denied Pinmemberaejw12:13 13 Nov '08  
Questionproblem using in an aspx button Pinmemberraziel781:58 2 Oct '08  
QuestionWorks on Windows XP Home? PinmemberHugo Tomas3:51 10 Jul '08  
AnswerRe: Works on Windows XP Home? Pinmemberaejw15:58 20 Jul '08  
QuestionHow do you install program Pinmemberchris guajardo11:50 18 Jun '08  
AnswerRe: How do you install program [modified] Pinmemberaejw12:59 19 Jun '08  
QuestionMultiple connections Exception PinmemberNickKa11:32 21 May '08  
AnswerRe: Multiple connections Exception Pinmemberaejw0:38 20 Jun '08  
GeneralRe: Multiple connections Exception PinmemberJosh Andy0:15 1 Jun '09  
GeneralMap netdriver error. [modified] PinmemberPaul Zhang17:24 16 Mar '08  
GeneralRe: Map netdriver error. Pinmemberaejw12:52 17 Mar '08  
GeneralRe: http webdrive (webdav) possible ? PinmemberMatterz2:09 11 Nov '07  
GeneralRe: http webdrive (webdav) possible ? Pinmemberaejw6:40 11 Nov '07  
GeneralRe: http webdrive (webdav) possible ? PinmemberMatterz18:31 26 Jan '08  
Questionhttp webdrive (webdav) possible ? PinmemberMatterz12:04 10 Nov '07  
AnswerRe: http webdrive (webdav) possible ? Pinmemberaejw18:54 10 Nov '07  
GeneralUnmapping Error PinmemberSturges6:12 9 Nov '07  
GeneralRe: Unmapping Error Pinmemberaejw19:00 10 Nov '07  

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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