ドライブの種類を取得する

注意:
この文書は以前「.NETでいきまっしょい!」で公開していたものですが、公開以降メンテナンスされていません。 今や古い情報となった内容が記載されている場合があるのでご注意ください。

 API関数のGetDriveTypeを使うことで、各ドライブの種類を取得できる。 また、使用可能なドライブはDirectory.GetLogicalDrives()メソッドで得ることができる。
VB.NET
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
Imports System.IO
Imports System.Runtime.InteropServices

Module Module1

    Public Enum DriveType As Integer
        Unknown = 0
        NoRootDir = 1
        Removable = 2
        Fixed = 3
        Remote = 4
        CDROM = 5
        RAMDisk = 6
    End Enum

    <DllImport("kernel32.dll", EntryPoint:="GetDriveType", CharSet:=CharSet.Auto)> _
    Function GetDriveType(<MarshalAs(UnmanagedType.LPTStr)> ByVal nDrive As String) As Integer
    End Function

    ' GetDriveTypeの戻り値
    'Const DRIVE_UNKNOWN As Integer = 0
    'Const DRIVE_NO_ROOT_DIR As Integer = 1
    'Const DRIVE_REMOVABLE As Integer = 2
    'Const DRIVE_FIXED As Integer = 3
    'Const DRIVE_REMOTE As Integer = 4
    'Const DRIVE_CDROM As Integer = 5
    'Const DRIVE_RAMDISK As Integer = 6

    ' ドライブの種類を取得する
    Function GetDriveTypeEx(ByVal drive As String) As DriveType

        Return CType(GetDriveType(drive), DriveType)

    End Function

    Sub Main()

        ' 使用可能なドライブを取得する
        Dim drives() As String
        Dim drive As String

        drives = Directory.GetLogicalDrives()

        For Each drive In drives

            Dim driveType As DriveType = GetDriveTypeEx(drive)
            Dim driveTypeString As String

            Select Case driveType

                Case driveType.Fixed : driveTypeString = "固定 ドライブ"
                Case driveType.Removable : driveTypeString = "リムーバブル ドライブ"
                Case driveType.CDROM : driveTypeString = "CD-ROM ドライブ"
                Case driveType.Remote : driveTypeString = "ネットワーク ドライブ"
                Case driveType.RAMDisk : driveTypeString = "RAM ディスク"
                Case driveType.NoRootDir : driveTypeString = "(ルートディレクトリが存在しません)"
                Case driveType.Unknown : driveTypeString = "(判別不能)"

            End Select

            Console.WriteLine("{0} - {1}", drive, driveTypeString)

        Next

    End Sub

End Module
C#
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
class Class1
{
    enum DriveType : int
    {
        Unknown = 0,
        NoRootDir = 1,
        Removable = 2,
        Fixed = 3,
        Remote = 4,
        CDROM = 5,
        RAMDisk = 6,
    }

    [DllImport( "kernel32.dll", EntryPoint = "GetDriveType", CharSet = CharSet.Auto )] 
    static extern int GetDriveType( [MarshalAs( UnmanagedType.LPTStr )] string nDrive );

    // GetDriveTypeの戻り値
    // const int DRIVE_UNKNOWN     = 0;
    // const int DRIVE_NO_ROOT_DIR = 1;
    // const int DRIVE_REMOVABLE   = 2;
    // const int DRIVE_FIXED       = 3;
    // const int DRIVE_REMOTE      = 4;
    // const int DRIVE_CDROM       = 5;
    // const int DRIVE_RAMDISK     = 6;

    // ドライブの種類を取得する
    static DriveType GetDriveTypeEx( string drive )
    {
        return (DriveType)GetDriveType( drive );
    }

    [STAThread]
    static void Main(string[] args)
    {
        // 使用可能なドライブを取得する
        string[] drives;

        drives = Directory.GetLogicalDrives();

        foreach ( string drive in drives )
        {
            DriveType driveType = GetDriveTypeEx( drive );
            string driveTypeString = null;

            switch ( driveType )
            {
                case DriveType.Fixed:
                    driveTypeString = "固定 ドライブ";
                    break;

                case DriveType.Removable:
                    driveTypeString = "リムーバブル ドライブ";
                    break;

                case DriveType.CDROM:
                    driveTypeString = "CD-ROM ドライブ";
                    break;

                case DriveType.Remote:
                    driveTypeString = "ネットワーク ドライブ";
                    break;

                case DriveType.RAMDisk:
                    driveTypeString = "RAM ディスク";
                    break;

                case DriveType.NoRootDir:
                    driveTypeString = "(ルートディレクトリが存在しません)";
                    break;

                case DriveType.Unknown:
                    driveTypeString = "(判別不能)";
                    break;
            }

            Console.WriteLine( "{0} - {1}", drive, driveTypeString );
        }
    }
}
出力結果
A:\ - リムーバブル ドライブ
C:\ - 固定 ドライブ
D:\ - 固定 ドライブ
E:\ - 固定 ドライブ
F:\ - 固定 ドライブ
G:\ - 固定 ドライブ
H:\ - 固定 ドライブ
I:\ - CD-ROM ドライブ
J:\ - CD-ROM ドライブ
Press any key to continue