我的 VB6 程序依赖于网络共享上的数据。无线网络上的 Win XP 在启动时通常无法重新连接映射的驱动器,因此它们处于断开状态。重新连接它们的唯一方法是在资源管理器中双击它们。
如何以编程方式执行此操作?是否有 API 调用可以做到这一点?
我的 VB6 程序依赖于网络共享上的数据。无线网络上的 Win XP 在启动时通常无法重新连接映射的驱动器,因此它们处于断开状态。重新连接它们的唯一方法是在资源管理器中双击它们。
如何以编程方式执行此操作?是否有 API 调用可以做到这一点?
您可以使用WNetAddConnection函数
Private Sub cmdMapDrive_Click()
Dim drive_letter As String
Dim share_name As String
Dim password As String
lblResult.Caption = "Working..."
Screen.MousePointer = vbHourglass
DoEvents
drive_letter = txtDriveLetter.Text
If InStr(drive_letter, ":") = 0 _
Then drive_letter = drive_letter & ":"
share_name = txtShareName.Text
password = txtPassword.Text
If WNetAddConnection(share_name, password, _
drive_letter) > 0 _
Then
lblResult.Caption = "Error mapping drive"
Else
lblResult.Caption = "Drive mapped"
End If
Screen.MousePointer = vbDefault
End Sub
代码来源:VB Helper
您可以使用 dos 命令 " " 并使用vb 中的 -commandnet use
启动它。shell
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/net_use.mspx?mfr=true
我已经这样做了Scripting.FileSystemObject
:
Public Function MapDrive(ByVal Sharename As String, DriveToMap As String) As Boolean
On Error GoTo Handler
Dim fso As Scripting.FileSystemObject
Dim ntwk As IWshRuntimeLibrary.IWshNetwork_Class
' Assume success; any failure will invoke the error handler & cause '
' the function to return false. '
MapDrive = True
Set fso = New Scripting.FileSystemObject
Set ntwk = New IWshRuntimeLibrary.IWshNetwork_Class
' If the specified drive doesn't even exist, just map it '
If Not fso.DriveExists(DriveToMap) Then
ntwk.MapNetworkDrive DriveToMap, Sharename
Exit Function
End If
' The drive already exists; see if it's already be mapped correctly. '
If UCase(fso.Drives(DriveToMap).ShareName) = UCase(Sharename) Then
Exit Function
End If
' The drive is mapped, but to the wrong place. Unmap, then map the drive. '
ntwk.RemoveNetworkDrive DriveToMap
ntwk.MapNetworkDrive DriveToMap, Sharename
Exit Function
Handler:
MapDrive = False
Err.Clear
End Function