Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have tried several ways to make this work and none have worked.

[Update]

This is one that I don't understand where to put the uncpath and drive letter.

VB
Public Declare Function WNetAddConnection2 _
    Lib "mpr.dll" Alias "WNetAddConnection2A" _
    (
        ByRef lpNetResource As NETRESOURCE,
        ByVal lpPassword As String,
        ByVal lpUserName As String,
        ByVal dwFlags As Integer) As Integer

Public Declare Function WNetCancelConnection2 _
    Lib "mpr" Alias "WNetCancelConnection2A" _
    (
        ByVal lpName As String,
        ByVal dwFlags As Integer,
        ByVal fForce As Integer) As Integer

<structlayout(layoutkind.sequential)>
Public Structure NETRESOURCE
    Public dwScope As Integer
    Public dwType As Integer
    Public dwDisplayType As Integer
    Public dwUsage As Integer
    Public lpLocalName As String
    Public lpRemoteName As String
    Public lpComment As String
    Public lpProvider As String
End Structure

'    Public Const ForceDisconnect As Integer = 1
Public Const RESOURCETYPE_DISK As Long = &H1
''' <summary>
''' Maps a UNC path to a specific drive.
''' </summary>
''' <param name="DriveLetter"></param>
''' <param name="UNCPath"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String) As Boolean

    Dim nr As NETRESOURCE
    Dim Username As String
    Dim Password As String

    nr = New NETRESOURCE
    nr.lpRemoteName = UNCPath
    nr.lpLocalName = DriveLetter & ":"
    Username = Nothing '(add parameters to pass this if necessary)
    Password = Nothing '(add parameters to pass this if necessary)
    nr.dwType = RESOURCETYPE_DISK

    Dim result As Integer
    result = WNetAddConnection2(nr, Password, Username, 0)

    If result = 0 Then
        Return True
    Else
        Return False
    End If
End Function



Here are the others

VB
Private Sub Command_Click()
        Dim startInfo As ProcessStartInfo
        startInfo = New System.Diagnostics.ProcessStartInfo("cmd.exe", """NET USE F: \\xxxxxxxxx\CSC Backup""")
    End Sub

Dim procID As Integer
Dim newProc As Diagnostics.Process
newProc = Diagnostics.Process.Start("C:\WINDOWS\SYSTEM32\NET.EXE")
procID = newProc.SessionId
procID = Shell("""net use F: \\xxxxxxx\csc backup")</structlayout(layoutkind.sequential)>
Posted
Updated 9-Oct-15 9:48am
v2
Comments
Richard Deeming 9-Oct-15 13:30pm    
Well how about telling us which ways you've tried, and how they've failed to work?

Otherwise, we'll just end up telling you the ways you've already tried.
Member 11866893 9-Oct-15 13:52pm    
This is one that I don't understand where to put the uncpath and drive letter.

Public Declare Function WNetAddConnection2 _
Lib "mpr.dll" Alias "WNetAddConnection2A" _
(
ByRef lpNetResource As NETRESOURCE,
ByVal lpPassword As String,
ByVal lpUserName As String,
ByVal dwFlags As Integer) As Integer

Public Declare Function WNetCancelConnection2 _
Lib "mpr" Alias "WNetCancelConnection2A" _
(
ByVal lpName As String,
ByVal dwFlags As Integer,
ByVal fForce As Integer) As Integer

<structlayout(layoutkind.sequential)>
Public Structure NETRESOURCE
Public dwScope As Integer
Public dwType As Integer
Public dwDisplayType As Integer
Public dwUsage As Integer
Public lpLocalName As String
Public lpRemoteName As String
Public lpComment As String
Public lpProvider As String
End Structure

' Public Const ForceDisconnect As Integer = 1
Public Const RESOURCETYPE_DISK As Long = &H1
'''
''' Maps a UNC path to a specific drive.
'''

''' <param name="DriveLetter"></param>
''' <param name="UNCPath"></param>
''' <returns>
''' <remarks>
Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String) As Boolean

Dim nr As NETRESOURCE
Dim Username As String
Dim Password As String

nr = New NETRESOURCE
nr.lpRemoteName = UNCPath
nr.lpLocalName = DriveLetter & ":"
Username = Nothing '(add parameters to pass this if necessary)
Password = Nothing '(add parameters to pass this if necessary)
nr.dwType = RESOURCETYPE_DISK

Dim result As Integer
result = WNetAddConnection2(nr, Password, Username, 0)

If result = 0 Then
Return True
Else
Return False
End If
End Function


Here are the others

Private Sub Command_Click()
Dim startInfo As ProcessStartInfo
startInfo = New System.Diagnostics.ProcessStartInfo("cmd.exe", """NET USE F: \\xxxxxxxxx\CSC Backup""")
End Sub

Dim procID As Integer
Dim newProc As Diagnostics.Process
newProc = Diagnostics.Process.Start("C:\WINDOWS\SYSTEM32\NET.EXE")
procID = newProc.SessionId
procID = Shell("""net use F: \\xxxxxxx\csc backup")
[no name] 9-Oct-15 15:59pm    
The quickest is: do not map. There is nowadays not a reason why to do this.... only my opinion...

1 solution

The quickest way? Don't map a drive at all. Use UNC paths instead of drive letters. You have to supply a UNC path to the NET USE command so you already have the path to start with.
 
Share this answer
 
Comments
Patrice T 9-Oct-15 21:25pm    
+5
Dave Kreskowiak 13-Oct-15 15:06pm    
The suggestions you got were warranted.

NO APPLICATION EVER maps network drives by itself unless that is it's only purpose. Drive letter assignments are always left up to administrators to avoid collisions like you're seem to want to do.

The process of mapping a network drive is a slow one. There is no "instant" way to do it. What you have already (shelling out to NET USE) is the poor mans way of doing it, but is no quicker nor slower than calling the Windows API functions yourself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900