获取正确的驱动器名称

时间:2017-04-19 13:32:57

标签: vb.net

我正在尝试获取映射网络驱动器的名称,但我尝试过的所有内容都会返回错误的数据。

在我的工作PC上我有5个驱动器。

C:\  which is displayed in Computer as OS (C:)
D:\  which is displayed in Computer as DVD RW Drive (D:) when no CD or DVD is in the drive.
T:\  which is displayed in Computer as IT--- (\\Homedrive) (T:)
V:\  which is displayed in Computer as Ste Moore (\\Homedrive\IT) (V:)
Z:\  which is displayed in Computer as dg.net (\\dg) (Z:)

当我使用System.IO.DriveInfo获取驱动器详细信息时,驱动器的卷标如下:

C:\   OS
D:\   Nothing as it is not ready
T:\   IT
V:\   IT
Z:\   DGNET

我知道T:\显示为IT ---因为它已重命名,但其他人都没有。

有没有办法获取计算机,此计算机,我的电脑或此电脑中显示的名称?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

感谢GSerg,您的链接提供了答案,即使它是在C#中。

对于其他寻找此信息而不了解C#的人来说,这里是VB.Net

<DllImport("shell32.dll", CharSet:=CharSet.Unicode)> _
Public Shared Function SHParseDisplayName(pszName As String, zero As IntPtr, <Out> ByRef ppidl As IntPtr, sfgaoIn As UInteger, <Out> ByRef psfgaoOut As UInteger) As UInteger
End Function

<DllImport("shell32.dll", CharSet:=CharSet.Unicode)> _
Public Shared Function SHGetNameFromIDList(pidl As IntPtr, sigdnName As SIGDN, <Out> ByRef ppszName As [String]) As UInteger
End Function

Public Enum SIGDN As UInteger
    NORMALDISPLAY = &H0
    PARENTRELATIVEPARSING = &H80018001UI
    DESKTOPABSOLUTEPARSING = &H80028000UI
    PARENTRELATIVEEDITING = &H80031001UI
    DESKTOPABSOLUTEEDITING = &H8004C000UI
    FILESYSPATH = &H80058000UI
    URL = &H80068000UI
    PARENTRELATIVEFORADDRESSBAR = &H8007C001UI
    PARENTRELATIVE = &H80080001UI
End Enum

Public Function GetDriveLabel(driveNameAsLetterColonBackslash As String) As String
    Dim pidl As IntPtr = Nothing
    Dim dummy As UInteger = Nothing
    Dim name As String = Nothing

    If SHParseDisplayName(driveNameAsLetterColonBackslash, IntPtr.Zero, pidl, 0, dummy) = 0 AndAlso
       SHGetNameFromIDList(pidl, SIGDN.PARENTRELATIVEEDITING, name) = 0 AndAlso Not name Is Nothing Then
        Return name
    End If

    Return Nothing
End Function