Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mapping Network Paths in VB 4

Status
Not open for further replies.

c2compliant

Programmer
May 11, 2000
11
US
Does anyone know how to check if a drive path is mapped and if not then map a network path on NT 4 using VB.  
 
Try This.Option Explicit<br><br>Private Const CONNECT_UPDATE_PROFILE = &H1<br><br>Private Const RESOURCE_CONNECTED As Long = &H1&<br>Private Const RESOURCE_GLOBALNET As Long = &H2&<br>Private Const RESOURCETYPE_DISK As Long = &H1&<br>Private Const RESOURCEDISPLAYTYPE_SHARE& = &H3<br>Private Const RESOURCEUSAGE_CONNECTABLE As Long = &H1&<br><br>Private Declare Function WNetAddConnection2 Lib &quot;mpr.dll&quot; _<br>&nbsp;&nbsp;Alias &quot;WNetAddConnection2A&quot; (lpNetResource As NETCONNECT, _<br>&nbsp;&nbsp;ByVal lpPassword As String, ByVal lpUserName As String, _<br>&nbsp;&nbsp;ByVal dwFlags As Long) As Long<br><br>Private Type NETCONNECT<br>&nbsp;&nbsp;&nbsp;dwScope As Long<br>&nbsp;&nbsp;&nbsp;dwType As Long<br>&nbsp;&nbsp;&nbsp;dwDisplayType As Long<br>&nbsp;&nbsp;&nbsp;dwUsage As Long<br>&nbsp;&nbsp;&nbsp;lpLocalName As String<br>&nbsp;&nbsp;&nbsp;lpRemoteName As String<br>&nbsp;&nbsp;&nbsp;lpComment As String<br>&nbsp;&nbsp;&nbsp;lpProvider As String<br>End Type<br><br><br><br><br><br>Public Function MapDrive(LocalDrive As String, _<br>&nbsp;&nbsp;RemoteDrive As String, Optional Username As String, _<br>&nbsp;&nbsp;Optional Password As String) As Boolean<br><br>'Example:<br>'MapDrive &quot;Q:&quot;, &quot;\\RemoteMachine\RemoteDirectory&quot;, _<br>'&quot;MyLoginName&quot;, &quot;MyPassword&quot;<br><br>&nbsp;&nbsp;&nbsp;Dim NetR As NETCONNECT<br><br>&nbsp;&nbsp;&nbsp;NetR.dwScope = RESOURCE_GLOBALNET<br>&nbsp;&nbsp;&nbsp;NetR.dwType = RESOURCETYPE_DISK<br>&nbsp;&nbsp;&nbsp;NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE<br>&nbsp;&nbsp;&nbsp;NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE<br>&nbsp;&nbsp;&nbsp;NetR.lpLocalName = Left$(LocalDrive, 1) & &quot;:&quot;<br>&nbsp;&nbsp;&nbsp;NetR.lpRemoteName = RemoteDrive<br><br>&nbsp;&nbsp;&nbsp;MapDrive = (WNetAddConnection2(NetR, Username, Password, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CONNECT_UPDATE_PROFILE) = 0)<br>&nbsp;&nbsp;&nbsp;<br><br>End Function<br>
 
Hi,

The above code only maps to a drive. Is there a way we can check if a particular path is already mapped before actually mapping it? If yes then how can we get the drive letter to which it is mapped ?

Thanks in Advance.
 
Code:
Dim objDrive As Object
Dim strLetter As String

For Each objDrive In CreateObject("Scripting.FileSystemObject").Drives
    If objDrive.DriveType = 3 Then
        If objDrive.ShareName = "\\server\share" Then
            'already exists
            strLetter = objDrive.DriveLetter
        End If
    End If
Next
 
Hi,

In the code above, when I try to map the drive as

for eg., \\server\share1\dir1\subdir1

It only looks for \\server\share1 to be correct. If the rest of the details like dir1 and subdir1 are correct and they exist then they are mapped. If either dir1 or subdir1 is incorrect, still it maps the path to a drive letter and when I try to open the mapped drive, it is blank. THis is happening when I try to map the unix paths to windows using the third party software SFU.

Is there a way I can capture the error if the information other than \\server\share is incorrect ?

Please advise how to handle this.

Thanks in Advance.
 
You can also use this:

Code:
Option Explicit

Private Sub Form_Load()
  Shell "c:\windows\system32\cmd.exe /c net use > d:\temp\netuse.txt"
End Sub

David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top