通过代码,我如何测试硬盘驱动器是否正在睡眠而不会唤醒它

时间:2021-06-24 16:52:18

I'm building a small app that gives me the free space available on my disks.

我正在构建一个小应用程序,它为我提供了磁盘上的可用空间。

I would like to add a feature displaying the status of the disk, if it is sleeping or not for example. The OS is Windows.

我想添加一个显示磁盘状态的功能,例如,如果它处于休眠状态。操作系统是Windows。

How can this be done? The code should not have to wake the disk to find out, of course ;)

如何才能做到这一点?当然,代码不应该唤醒磁盘以找出;)

A solution in C# would be nice but I guess any solution will do...

C#中的解决方案会很好,但我猜任何解决方案都会...

Thank you for you help.

谢谢你的帮助。

2 个解决方案

#1


C++ solution (call GetDiskPowerState and it will iterate over physical drives until there are no more):

C ++解决方案(调用GetDiskPowerState,它将迭代物理驱动器,直到没有更多):

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;
}

#2


And in C# (From http://msdn.microsoft.com/en-us/library/ms704147.aspx)

在C#中(来自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;
}

#1


C++ solution (call GetDiskPowerState and it will iterate over physical drives until there are no more):

C ++解决方案(调用GetDiskPowerState,它将迭代物理驱动器,直到没有更多):

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;
}

#2


And in C# (From http://msdn.microsoft.com/en-us/library/ms704147.aspx)

在C#中(来自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;
}