現在のデバイス名マッピングを取得する方法を記載します。
取得には、Win32APIを使用します。
Imports System.Runtime.InteropServices
<DllImport("Kernel32.dll")>
Public Shared Function QueryDosDevice(lpDeviceName As String, lpTargetPath As System.Text.StringBuilder, ucchMax As Integer) As Integer
End Function
''' <summary>
''' デバイス名マッピングを取得します。
''' </summary>
''' <param name="lpDeviceName"></param>
''' <returns></returns>
Public Function GetDriveMapping(lpDeviceName As String) As String
Dim sb = New System.Text.StringBuilder(1024 * 64)
QueryDosDevice(lpDeviceName, sb, sb.Capacity)
Return sb.ToString()
End Function
・上記でsbに64KByte割り当てていますが、そこまでの領域は必要ありませんが、領域が少なすぎると、取得できませんので注意してください。
呼び出し側
For i = 0 To 25
Dim drv = Chr(&H41 + i) + ":"
Dim s = GetDriveMapping(drv)
If s <> "" Then
Console.WriteLine("{0} {1}", drv, s)
End If
Next
表示結果
C: \Device\HarddiskVolume2 D: \Device\HarddiskVolume4 E: \Device\HarddiskVolume7
上記のようなものが表示されます。
・QueryDosDeviceのlpDeviceNameには、A:~Z: 以外にも”COM1″ や “Volume{GUID}” なども指定できます。
※Volume{GUID}の例としては”Volume{0f0eee7d-39d3-11e6-9ce5-001583544e78}”などGUIDが変わります。
・ドライブのGUIDを調べるには、コマンドプロンプトで
Mountvol
[Enter]キーを押すと表示されます。