2011-03-04 15 views
9

cómo obtener todas las unidades en una PC. y tipos de cada unidad y el espacio libre de cadaCómo obtener todas las unidades en PC con .NET usando C#

es decir, Sistema-Drive, CD-Drive, unidad DVD, extraíble, etc ...

Si un sistema está conectado con una nueva unidad pueden ser un pen drive o disco duro externo.

¿Cómo detectarlos en el momento del archivo adjunto?

Respuesta

13

Para obtener una lista de las unidades, puede utilizar la clase System.IO.DriveInfo:

foreach(var drive in DriveInfo.GetDrives()) 
{ 
    Console.WriteLine("Drive Type: {0}", drive.DriveType); 
    Console.WriteLine("Drive Size: {0}", drive.TotalSize); 
    Console.WriteLine("Drive Free Space: {0}", drive.TotalFreeSpace); 
} 

Por desgracia, esto no proporciona una manera de escuchar los puntos de inserción clave USB . Hay otra cuestión se trata de que se puede consultar:

.NET - Detecting USB Drive Insertion and Removal...

0

Aquí está la respuesta a la primera pregunta. Para obtener todas las unidades lógicas se puede probar esto:

string[] drives = Environment.GetLogicalDrives(); 
0

La clase System.IO.DriveInfo es el lugar para comenzar. La propiedad DriveType puede decirle lo que está buscando.

2

Usted puede obtener información de las unidades y con bastante facilidad

DriveInfo[] drives = DriveInfo.GetDrives(); 

foreach (DriveInfo drive in drives) 
{ 
    Console.WriteLine(drive.Name); 
    Console.WriteLine(drive.TotalSize); 
} 

Hay un buen artículo sobre la detección de añadir/quitar unidades on CodeProject

+0

Parece como si estuviera demasiado lento ... –

+0

@Phil No hay mucho en ella ! –

3
 public string getalldrivestotalnfreespace() 
    { 
     string s = " Drive   Free Space TotalSpace  FileSystem %Free Space  DriveType\n\r========================================================================================\n\r"; 
     foreach (DriveInfo drive in DriveInfo.GetDrives()) 
     { 
      double ts = 0; 
      double fs = 0; 
      double frprcntg = 0; 
      long divts = 1024 * 1024 * 1024; 
      long divfs = 1024 * 1024 * 1024; 
      string tsunit = "GB"; 
      string fsunit = "GB"; 
      if (drive.IsReady) 
      { 
       fs = drive.TotalFreeSpace; 
       ts = drive.TotalSize; 
       frprcntg = (fs/ts) * 100; 
       if (drive.TotalSize < 1024) 
       { 
        divts =1; tsunit = "Byte(s)"; 
       } 
       else if (drive.TotalSize < (1024*1024)) 
       { 
        divts = 1024; tsunit = "KB"; 
       } 
       else if (drive.TotalSize < (1024 * 1024*1024)) 
       { 
        divts = 1024*1024; tsunit = "MB"; 
       } 
       //---------------------- 
       if (drive.TotalFreeSpace < 1024) 
       { 
        divfs = 1; fsunit = "Byte(s)"; 
       } 
       else if (drive.TotalFreeSpace < (1024 * 1024)) 
       { 
        divfs = 1024; fsunit = "KB"; 
       } 
       else if (drive.TotalFreeSpace < (1024 * 1024 * 1024)) 
       { 
        divfs = 1024 * 1024; fsunit = "MB"; 
       } 
       s = s + 
       " " + drive.VolumeLabel.ToString() + 
       "[" + drive.Name.Substring(0, 2) + 
       "]\t" + String.Format("{0,10:0.0}", ((fs/divfs)).ToString("N2")) + fsunit + 
       String.Format("{0,10:0.0}", (ts/divts).ToString("N2")) + tsunit + 
       "\t" + drive.DriveFormat.ToString() + "\t\t" + frprcntg.ToString("N2") + "%"+ 
       "\t\t" + drive.DriveType.ToString(); 

       s = s + "\n\r"; 
      } 
     } 
     return s; 
    } 

que la salida debe ser similar a: - enter image description here

+1

Solo menciono: en realidad puede incluir una imagen en su publicación simplemente subiéndola (haga clic en el icono "insertar imagen". De esta manera, puede aparecer en línea en lugar de tener que seguir su enlace). – Mic

Cuestiones relacionadas