2009-05-13 17 views
8

Estoy construyendo una pequeña aplicación que me da el espacio libre disponible en mis discos.Por código, ¿cómo puedo probar si una unidad de disco duro está durmiendo sin activarlo?

Me gustaría agregar una característica que muestra el estado del disco, si está inactivo o no, por ejemplo. El sistema operativo es Windows.

¿Cómo se puede hacer esto? El código no debería tener que despertar el disco para averiguar, por supuesto;)

Una solución en C# sería bueno, pero supongo que cualquier solución va a hacer ...

Gracias por su ayuda.

Respuesta

6

C solución ++ (llamada GetDiskPowerState y va a iterar sobre unidades físicas hasta que no hay más):

class AutoHandle 
{ 
    HANDLE mHandle; 
public: 
    AutoHandle() : mHandle(NULL) { } 
    AutoHandle(HANDLE h) : mHandle(h) { } 

    HANDLE * operator &() 
    { 
     return &mHandle; 
    } 

    operator HANDLE() const 
    { 
     return mHandle; 
    } 

    ~AutoHandle() 
    { 
     if (mHandle && mHandle != INVALID_HANDLE_VALUE) 
      ::CloseHandle(mHandle); 
    } 
}; 


bool 
GetDiskPowerState(LPCTSTR disk, string & txt) 
{ 
    AutoHandle hFile = CreateFile(disk, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 
    if (hFile && hFile != INVALID_HANDLE_VALUE) 
    { 
     BOOL powerState = FALSE; 
     const BOOL result = GetDevicePowerState(hFile, &powerState); 
     const DWORD err = GetLastError(); 

     if (result) 
     { 
      if (powerState) 
      { 
       txt += disk; 
       txt += " : powered up\r\n"; 
      } 
      else 
      { 
       txt += disk; 
       txt += " : sleeping\r\n"; 
      } 
      return true; 
     } 
     else 
     { 
      txt += "Cannot get drive "; 
      txt += disk; 
      txt += "status\r\n"; 
      return false; 
     } 
    } 

    return false; 
} 

string 
GetDiskPowerState() 
{ 
    string text; 
    CString driveName; 
    bool result = true; 
    for (int idx= 0; result; idx++) 
    { 
     driveName.Format("\\\\.\\PhysicalDrive%d", idx); 
     result = GetDiskPowerState(driveName, text); 
    } 
    return text; 
} 
+0

Gracias, funciona como un encanto. – Jonx

3

Y en C# (De http://msdn.microsoft.com/en-us/library/ms704147.aspx)

// Get the power state of a harddisk 
string ReportDiskStatus() 
{ 
    string status = string.Empty; 
    bool fOn = false; 

    Assembly assembly = Assembly.GetExecutingAssembly(); 
    FileStream[] files = assembly.GetFiles(); 
    if (files.Length > 0) 
    { 
     IntPtr hFile = files[0].Handle; 
     bool result = GetDevicePowerState(hFile, out fOn); 
     if (result) 
     { 
      if (fOn) 
      { 
       status = "Disk is powered up and spinning"; 
      } 
      else 
      { 
       status = "Disk is sleeping"; 
      } 
     } 
     else 
     { 
      status = "Cannot get Disk Status"; 
     } 
     Console.WriteLine(status); 
    } 
    return status; 
} 
+0

Sí, bueno ... Le falta la importación pero el enlace lo da todo. Gracias. – Jonx

Cuestiones relacionadas