Come individuare dispositivi di archiviazione USB e unità CD/DVD scrivibili (C#)

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

  •  09-06-2019
  •  | 
  •  

Domanda

Come posso scoprire eventuali dispositivi di archiviazione USB e/o masterizzatori CD/DVD disponibili in un dato momento (utilizzando C# .Net2.0).

Vorrei offrire agli utenti una scelta di dispositivi su cui è possibile archiviare un file per la rimozione fisica, ad es.non il disco rigido.

È stato utile?

Soluzione

using System.IO;

DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
  if (d.IsReady && d.DriveType == DriveType.Removable)
  {
    // This is the drive you want...
  }
}

La documentazione della classe DriveInfo è qui:

http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx

Altri suggerimenti

questo è il codice VB.NET per verificare la presenza di eventuali unità rimovibili o unità CD-ROM collegate al computer:

Me.lstDrives.Items.Clear()
For Each item As DriveInfo In My.Computer.FileSystem.Drives
    If item.DriveType = DriveType.Removable Or item.DriveType = DriveType.CDRom Then
        Me.lstDrives.Items.Add(item.Name)
    End If
Next

non sarà così difficile modificare questo codice in un equivalente C# e altro ancora tipo di guidasono disponibili.
Da MSDN:

  • Sconosciuto: Il tipo di unità è sconosciuto.
  • Nessuna directory radice: L'unità non dispone di una directory root.
  • Rimovibile: L'unità è un dispositivo di archiviazione rimovibile, ad esempio un'unità floppy disk o un'unità flash USB.
  • Fisso: L'unità è un disco fisso.
  • Rete: L'unità è un'unità di rete.
  • Cd rom: L'unità è un dispositivo disco ottico, ad esempio un CD o un DVD-ROM.
  • Ariete: L'unità è un disco RAM.

in C# puoi ottenere lo stesso utilizzando la classe System.IO.DriveInfo

using System.IO;

public static class GetDrives
{
    public static IEnumerable<DriveInfo> GetCDDVDAndRemovableDevices()
    {
        return DriveInfo.GetDrives().
            Where(d => d.DriveType == DriveType.Removable
            && d.DriveType == DriveType.CDRom);
    }

}

Questo è un modulo completo per VB.NET:
Importa System.IO
Modulo OttieniNomiUnitàPerTipo
Funzione GetDriveNames (facoltativo ByVal DType As DriveType = DriveType.Removable) Come ListBox
Per ogni DN come System.IO.DriveInfo in My.Computer.FileSystem.Drives
Se DN.DriveType = DType Allora
GetDriveNames.Items.Add(DN.Nome)
Finisci se
Prossimo
Fine funzione
Fine modulo

'Drive Types <br>
'Unknown: The type of drive is unknown. <br>
'NoRootDirectory: The drive does not have a root directory. <br>
'Removable: The drive is a removable storage device, such as a floppy disk drive or a USB flash drive. <br>
'Fixed: The drive is a fixed disk. <br>
'Network: The drive is a network drive. <br>
'CDRom: The drive is an optical disc device, such as a CD or DVD-ROM. <br>
'Ram: The drive is a RAM disk. <br>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top