Come rilevare se un'unità specifica è un disco rigido?
-
02-07-2019 - |
Domanda
In C # come si rileva che un'unità specifica è un disco rigido, unità di rete, CD-ROM o floppy?
Soluzione
Il metodo GetDrives () restituisce una classe DriveInfo che ha una proprietà DriveType che corrisponde all'enumerazione di System.IO.DriveType:
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.
}
Ecco un esempio leggermente modificato di MSDN che mostra le informazioni per tutte le unità:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}, Type {1}", d.Name, d.DriveType);
}
Altri suggerimenti
DriveInfo.DriveType dovrebbe funzionare per te.
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
}
Controlla la System.IO.DriveInfo class e proprietà DriveType.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow