VB6:如何以编程方式重新连接断开连接的映射驱动器
我的VB6程序依赖于网络共享上的数据。无线网络上的Win XP通常无法在启动时重新连接映射驱动器,因此它们处于断开连接状态。重新连接它们的唯一方法是在资源管理器中双击它们VB6:如何以编程方式重新连接断开连接的映射驱动器,vb6,windows-xp,mapped-drive,Vb6,Windows Xp,Mapped Drive,我的VB6程序依赖于网络共享上的数据。无线网络上的Win XP通常无法在启动时重新连接映射驱动器,因此它们处于断开连接状态。重新连接它们的唯一方法是在资源管理器中双击它们 如何以编程方式执行此操作?是否有API调用可以执行此操作?您可以使用dos命令“net use”并使用vb中的shell-命令启动它 您可以使用该功能 代码源:我已经通过脚本编写完成了这项工作。FileSystemObject: Public Function MapDrive(ByVal Sharename As Strin
如何以编程方式执行此操作?是否有API调用可以执行此操作?您可以使用dos命令“
net use
”并使用vb中的shell
-命令启动它
您可以使用该功能
代码源:我已经通过
脚本编写完成了这项工作。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
我相信在这里,WNetUseConnection通常是一个更好的选择。
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