GetDevicePowerState (kernel32)
Last changed: -62.107.93.107

.
Summary

C# Signature:

[DllImport("kernel32.dll")]
static extern bool GetDevicePowerState(IntPtr hDevice, out bool pfOn);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

// Get the power state of a harddisk (From http://msdn.microsoft.com/en-us/library/ms704147.aspx)

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;

}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation