#2
moou2018-08-15 18:11
|
程序代码:
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.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer
Public Const RESOURCE_CONNECTED = &H1
Public Const RESOURCETYPE_ANY = &H0
Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Structure NETRESOURCE
Dim dwScope As Integer
Dim dwType As Integer
Dim dwDisplayType As Integer
Dim dwUsage As Integer
Dim lpLocalName As String
Dim lpRemoteName As String
Dim lpComment As String
Dim lpProvider As String
End Structure
'连接网络磁盘函数
Public Function netconnect(ByVal localDrive As String, ByVal sharePath As String, ByVal userName As String, ByVal UserPassword As String, ByVal Juanbiao As String) As Boolean
Dim NetR As New NETRESOURCE
Dim ErrInfo As Integer
NetR.dwScope = RESOURCE_CONNECTED
NetR.dwType = RESOURCETYPE_ANY
NetR.dwDisplayType = RESOURCETYPE_ANY
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
'设置驱动器
NetR.lpLocalName = localDrive
'设置远程端口名字
NetR.lpRemoteName = sharePath
'这里尝试修改这个参数,看看能不能修改卷标
NetR.lpComment = Juanbiao
NetR.lpProvider = ""
ErrInfo = WNetAddConnection2(NetR, UserPassword, userName, 1)
If ErrInfo = 0 Then
Return True
Else
Return False
End If
End Function
'断开网络磁盘函数
Public Function netdisconnect(ByVal localDrive As String) As Boolean
Dim ErrInfo As Integer
ErrInfo = WNetCancelConnection2(localDrive, 1, False)
If ErrInfo = 0 Then
Return True
Else
Return False
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Netpatch As String ="\\192.168.0.250\A001"
Dim Username As String ="A001"
Dim password As String ="123"
Dim Netdisk As String ="X:"
'这里尝试传递卷标给参数 NetR.lpComment 可是无效
Dim Diskname As String = "个性化磁盘"
If netconnect(Netdisk, Netpatch, Username, password, Diskname) = True Then
MsgBox("磁盘连接成功!")
Me.Close()
Else
MsgBox("磁盘连接失败!")
End If
End Sub