CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    mapping local disk folder to drive letter

    I have VB 6.0 and need to map a folder on the local disk to a drive letter: i.e. c:\myapp\mydata would need to be mapped to e:\mydata.

    Mapping network shares is rather easy, but I cannot find out how to do this. Can anyone help? Examples would be really benificial.
    There are 10 types of people in the world, those that understand binary and those that don't.
    Using VS 2010

  2. #2
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: mapping local disk folder to drive letter

    Do you mean

    Rightclick - Start
    RightClick on My Computer
    Click on Map Network drive

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: mapping local disk folder to drive letter

    If you are talking about doing that in VB 6.0 then you can use the API functions

    here is a sample code
    Code:
    
    Option Explicit
    
    Declare Function WNetAddConnection Lib "mpr.dll" Alias _
    "WNetAddConnectionA" (ByVal lpszNetPath As String, _
    ByVal lpszPassword As String, ByVal lpszLocalName _
    As String) As Long 
    
    Declare Function WNetCancelConnection Lib "mpr.dll" _
    Alias "WNetCancelConnectionA" (ByVal lpszName _
    As String, ByVal bForce As Long) As Long 
    
    Const WN_SUCCESS = 0 ' The function was successful.
    Const WN_NET_ERROR = 2 ' An error occurred on the network.
    Const WN_BAD_PASSWORD = 6 ' The password was invalid.
    
    Function AddConnection(MyShareName As String, _
    MyPWD As String, UseLetter As String) As Integer 
    
    On Local Error GoTo AddConnection1_Err
    
    AddConnection = WNetAddConnection(MyShareName, _
    MyPWD, UseLetter)
    AddConnection_End: 
    
    Exit Function 
    
    AddConnection_Err:
    AddConnection = Err
    MsgBox Error$ 
    
    Resume AddConnection_End 
    
    End Function 
    
    Function CancelConnection(DriveLetter As String, _
    Force As Integer) As Integer 
    
    On Local Error GoTo CancelConnection_Err
    CancelConnection = WNetCancelConnection(DriveLetter, _
    Force)
    CancelConnection_End: 
    
    Exit Function 
    
    CancelConnection_Err:
    CancelConnection = Err
    MsgBox Error$ 
    
    Resume CancelConnection_End End Function


    if you are trying to map the shared folder through Desktop, then check the solution given by George....

  4. #4
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: mapping local disk folder to drive letter

    Not sure if this is what you are looking for but.

    subst e: c:\myapp

  5. #5
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    Re: mapping local disk folder to drive letter

    I am trying to do this by using the code given by "VB_THE_BEST". If I wanted help with manually mapping a drive, I would have asked elewhere.

    When i use the WNetAddConnection method given (which I have been trying to use for the past several days) I am getting the return code 67,
    ERROR_BAD_NET_NAME, The network name cannot be found.
    I am passing in the following data:
    Code:
    Function setDrive(sDrive As String, sShare As String) As Boolean
    On Local Error GoTo setDrive_err
        Dim sPassword As String
        Dim res As Boolean
        Dim val As Integer
        res = False    
    
        sShare = "C:\data"
        sPassword = ""
        sDrive = "k:"
    
        val = WNetAddConnection(sShare, sPassword, sDrive)
        res = IIf(val = 0, True, False)
        
    setDrive_end:
        
        setDrive = val
    Exit Function
    
    setDrive_err:
    
    setDrive = Err
    MsgBox Error$
    Resume setDrive_end
    
    End Function
    I do not understand why I am getting the code 67?

    I also need to ensure that local folders like "c:\documents and settings\<-username->\my documents\my app data" can be used (note spaces in name). Is this possible?
    There are 10 types of people in the world, those that understand binary and those that don't.
    Using VS 2010

  6. #6
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    Re: mapping local disk folder to drive letter (SOLVED)

    SOLVED!!!

    I had to use WNetAddConnection2, and I also need to use the local drive default share UNC path like the following:

    c:\ has a default share of C$, if tyhe local machine name is programmer, then use '\\programmer\c$\documents and settings\myname\my documents' as the shared path. Works great on the local machine!!!
    There are 10 types of people in the world, those that understand binary and those that don't.
    Using VS 2010

  7. #7
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: mapping local disk folder to drive letter

    oh sorry...

    i thought u meant to map a network drive...

    well to Substitute a Drive Letter for a particular folder on the System, you will have to use a different API

    here is code.. put this in a module

    Code:
    Private Declare Function DefineDosDevice Lib "kernel32" _
    		Alias "DefineDosDeviceA" _
    	 (ByVal dwFlags As Long, _
    		ByVal lpDeviceName As String, _
    		Optional ByVal lpTargetPath As String = vbNullString) As Long
    Private Const DDD_REMOVE_DEFINITION As Long = &H2
    Private Sub Main()
    	'Add the subst:
    	If DefineDosDevice(0&, "z:", "C:\Documents and Settings\myUserName") = 0 Then
    		MsgBox "Failed: " & Err.LastDllError
    	End If
    	'Delete the subst:
    	If DefineDosDevice(DDD_REMOVE_DEFINITION, "z:") = 0 Then
    		MsgBox "Failed: " & Err.LastDllError
    	End If
    End Sub
    you can substitute any folder on the hard-disk....

    hope this one helps...

    PS: I have used this on Windows NT/2000, but not on WinXP, so u will have to test it on XP..

    Edit... In the mean time i have found an interesting link...
    this is definetly going to help u..
    http://www.planet-source-code.com/vb...49761&lngWId=1
    Last edited by vb_the_best; April 25th, 2005 at 09:40 AM.

  8. #8
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    Re: mapping local disk folder to drive letter

    Thanks for yur help on this. Mapping to network drives is easy, I have been doing that for awhile now. Working with the local drives was a little more difficult but using the UNC path (\\comname\c$\<path>) really works well since the C$ is a valid network share path, albiet hidden.

    I tried to google on DefineDosDevice but got really sketchy info, and I am not altogether sure it is supported with XP. It looks as though MS removed it's links on MSDN to it. I think I will stay away from it.

    Once again, you helped push me in the right direction and you input is GREATLY appreciated!!!
    There are 10 types of people in the world, those that understand binary and those that don't.
    Using VS 2010

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured