ヘッダー
Visual Basic サンプル集
VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

ドライブの種類を判断する

2021/5/9

→ C# のサンプルに切り替える

 

パスからドライブの種類を判断する

VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

Dim path As String = "E:\test\abc.mp3"

Dim driveName As String = IO.Path.GetPathRoot(path)
Dim drive As New IO.DriveInfo(driveName)

Select Case drive.DriveType
    Case IO.DriveType.CDRom
        Debug.WriteLine(driveName & "はCD/DVD/Blu-rayなどの光学ドライブです。")
    Case IO.DriveType.Fixed
        Debug.WriteLine(driveName & "はSSDやハードディスクなどの固定ディスクです。")
    Case IO.DriveType.Network
        Debug.WriteLine(driveName & "はネットワークドライブです。")
    Case IO.DriveType.NoRootDirectory
        Debug.WriteLine(driveName & "は何もマウントされていないドライブなどです。")
    Case IO.DriveType.Ram
        Debug.WriteLine(driveName & "はRAMディスクです。")
    Case IO.DriveType.Removable
        Debug.WriteLine(driveName & "はUSBメモリーやSDカード、フロッピーディスクなどのリムーバブルドライブです。")
    Case IO.DriveType.Unknown
        Debug.WriteLine(driveName & "のドライブの種類は不明です。")
End Select

Debug.WriteLineが表示される場所

メモ:調べるパスが実在している必要はありません。 存在しないドライブを指定すると NoRootDirectory と判断されます。

 

 

ドライブの一覧とその種類を列挙する

VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

For Each driveName As String In IO.Directory.GetLogicalDrives
    Dim drive As New IO.DriveInfo(driveName)
    Debug.WriteLine($"{driveName} = {drive.DriveType}")
    'Debug.WriteLine(driveName & " = " & drive.DriveType.ToString) '←VB2013以前の場合
Next

Debug.WriteLineが表示される場所

メモ:出力されるドライブの種類 Fixed や Removable などの意味は、上述の例を参照。

Windowsでの実行結果の例

C:\ = Fixed
D:\ = Fixed
E:\ = Removable
F:\ = Removable

Linux(Debian)での実行結果の例(抜粋)

/sys = Ram
/proc = Ram
/dev = Ram
/dev/pts = Ram
/run = Ram
/ = Fixed
/sys/kernel/security = Ram
/dev/shm = Ram

 

 

VB.NET 2003以前の場合

VB.NET 2003以前では DriveInfo クラスを使用できないため、Windows APIを使用します。

この例では、ドライブの一覧とその種類を列挙します。種類は 2, 3などの数字で出力されます。数字の意味は DriveType 列挙型 の表でわかります。

VB.NET2002対応 VB.NET2003対応 VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal lpRootPathName As String) As Integer
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Integer, <Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedType.LPArray)> ByVal lpBuffer() As Byte) As Integer

Public Sub ListDrives()

    Dim buffer(255) As Byte
    Dim logicalDrives As New System.Collections.ArrayList

    '有効なドライブの一覧を取得する。
    Call GetLogicalDriveStrings(buffer.Length, buffer)

    Dim temp As String = ""
    Dim b As Byte
    For Each b In buffer
        If b = 0 Then
            If Len(temp) > 0 Then
                logicalDrives.Add(temp)
                temp = ""
            End If
        Else
            temp = temp & Convert.ToChar(b)
        End If
    Next

    Dim driveName As String
    For Each driveName In logicalDrives
        Dim driveType As Integer = GetDriveType(driveName)
        Debug.WriteLine(driveName & " = " & driveType.ToString)
    Next
End Sub

Debug.WriteLineが表示される場所

 

 

参考

DriveType 列挙型 (System.IO) | Microsoft Docs

GetDriveTypeA function (fileapi.h) - Win32 apps | Microsoft Docs

 


VB6対応 VB6では FileSystemObjectを使用します。