特定のドライブがハードドライブかどうかを検出する方法は?

StackOverflow https://stackoverflow.com/questions/148742

  •  02-07-2019
  •  | 
  •  

質問

C#では、特定のドライブがハードドライブ、ネットワークドライブ、CDRom、またはフロッピーであることをどのように検出しますか?

役に立ちましたか?

解決

GetDrives()メソッドは、System.IO.DriveTypeの列挙に対応するDriveTypeプロパティを持つDriveInfoクラスを返します。

public enum DriveType
{
    Unknown,         // The type of drive is unknown.  
    NoRootDirectory, // The drive does not have a root directory.  
    Removable,       // The drive is a removable storage device, 
                     //    such as a floppy disk drive or a USB flash drive.  
    Fixed,           // The drive is a fixed disk.  
    Network,         // The drive is a network drive.  
    CDRom,           // The drive is an optical disc device, such as a CD 
                     // or DVD-ROM.  
    Ram              // The drive is a RAM disk.   
}

すべてのドライブの情報を表示するMSDNのわずかに調整された例を次に示します。

    DriveInfo[] allDrives = DriveInfo.GetDrives();
    foreach (DriveInfo d in allDrives)
    {
        Console.WriteLine("Drive {0}, Type {1}", d.Name, d.DriveType);
    }

他のヒント

DriveInfo.DriveType は機能するはずですあなたのために。

DriveInfo[] allDrives = DriveInfo.GetDrives();

foreach (DriveInfo d in allDrives)
{
    Console.WriteLine("Drive {0}", d.Name);
    Console.WriteLine("  File type: {0}", d.DriveType);
}

System.IO.DriveInfo クラスを確認します。およびDriveTypeプロパティ。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top