vb.net如何检查网络驱动器是否持久映射

发布于 2024-08-07 22:48:01 字数 391 浏览 9 评论 0原文

我使用以下代码在系统上布局网络驱动器。我想添加第三列以实现持久性,但在 vb.net 中我不知道如何检查驱动器是否具有持久性映射。有什么建议吗?

For Each drive_info As DriveInfo In DriveInfo.GetDrives()
        If drive_info.DriveType().ToString = "Network" Then
            With maps.Items.Add(drive_info.Name)
                .SubItems.Add(drive_info.DriveType().ToString)
            End With
        End If
    Next drive_info

I use the following code to layout network drives on a system. I want to add a third column for persistence but in vb.net I do not know how to check if a drive has a persistent map or not. Any suggestions?

For Each drive_info As DriveInfo In DriveInfo.GetDrives()
        If drive_info.DriveType().ToString = "Network" Then
            With maps.Items.Add(drive_info.Name)
                .SubItems.Add(drive_info.DriveType().ToString)
            End With
        End If
    Next drive_info

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

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

发布评论

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

评论(2

梨涡少年 2024-08-14 22:48:01

您本来可以在 WMI 中完成此操作,而无需任何(好吧,更少)令人讨厌的麻烦。

例如,

Imports System
Imports System.Management

Public Module modmain
   Sub Main()
    Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_NetworkConnection WHERE LocalName = 'Z:'")
    Dim obj As ManagementObject
    For Each obj In searcher.Get
        Console.WriteLine("{0} {1}", obj.Item("LocalName").ToString, obj.Item("Persistent"))
    Next
   End Sub
End Module

显然您需要添加对 System.Management.dll 的引用并将 Z: 更改为您正在检查的驱动器,或者您可能可以仅使用该代码片段替换所有代码,因为删除 WHERE 子句将返回所有映射的驱动器。

You could have always done it in WMI without any (well okay fewer) nasty cludges.

e.g.

Imports System
Imports System.Management

Public Module modmain
   Sub Main()
    Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_NetworkConnection WHERE LocalName = 'Z:'")
    Dim obj As ManagementObject
    For Each obj In searcher.Get
        Console.WriteLine("{0} {1}", obj.Item("LocalName").ToString, obj.Item("Persistent"))
    Next
   End Sub
End Module

Obviously you need to add a reference to System.Management.dll and change Z: to the drive you are checking, or you could probably replace all your code with just that snippet as removing the WHERE clause will return all mapped drives.

第七度阳光i 2024-08-14 22:48:01

这可能对你有帮助。它是一个枚举网络资源的C#类,并且具有区分持久连接的能力:

http://www.codeproject.com/KB/cs/csenumnetworkresources.aspx?msg=964694

我很抱歉它是用 C# 编写的,但它做了一些我不知道的事情,比如整理内存VB中怎么做。

常量被传递到 EnumerateServers 函数以提供对输出的精细控制。您会发现感兴趣的常量是:

RESOURCE_REMEMBERED 

枚举记住的(持久)连接。

This might help you. It is a C# class that enumerates network resources, and has the ability to distinguish persistent connections:

http://www.codeproject.com/KB/cs/csenumnetworkresources.aspx?msg=964694

I apologize that it is in C#, but it does some things like marshaling memory that I don't know how to do in VB.

Constants are passed to the EnumerateServers function to provide fine control of the output. The constant you would find of interest is:

RESOURCE_REMEMBERED 

Enumerates remembered (persistent) connections.

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