VB6:如何以编程方式重新连接断开的映射驱动器

发布于 2024-10-23 13:53:09 字数 139 浏览 1 评论 0原文

我的 VB6 程序依赖于网络共享上的数据。无线网络上的 Win XP 通常无法在启动时重新连接映射的驱动器,因此它们处于断开状态。重新连接它们的唯一方法是在资源管理器中双击它们。

我怎样才能以编程方式做到这一点?是否有 API 调用可以做到这一点?

My VB6 program relies on data being on a network share. Win XP on a wireless network often cannot reconnect mapped drives at startup so they are in a disconnected state. The only way to reconnect them is to double-click on them in Explorer.

How can I do this programmatically? Is there an API call that will do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧街凉风 2024-10-30 13:53:10

您可以使用 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

You can use the WNetAddConnection function

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

Code Source: VB Helper

黎歌 2024-10-30 13:53:10

您可以使用 dos 命令“net use”并使用 vb 中的 shell 命令启动它。

http://www .microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/net_use.mspx?mfr=true

You can use the dos command "net use" and start it with the shell-command from vb.

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/net_use.mspx?mfr=true

娜些时光,永不杰束 2024-10-30 13:53:10

我已经使用 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

I've done this with the 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文