最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

S.M.A.R.T Disk remaining life percentage lifetime used C# - Stack Overflow

programmeradmin3浏览0评论

I want to retrieve or calculate this value "remaining life" that can be found in Windows 11 at storage setting, or in CrystalDiskInfo as "Health Status" here i have 99% in windows it is also 99%

Here i have all attributes i could use, but it does not contain something "life" or "time" related

I tried calculate it mannualy via summing attributes with coefficients but it didnt lead to good result, so how can i calculate or extract this value ? It should work on different disks regarding manufacturer.

Using Get-PhysicalDisk | Get-StorageReliabilityCounter | Select-Object Wear in PowerShell gives me 0, but it should be 1 at least i guess

I want to retrieve or calculate this value "remaining life" that can be found in Windows 11 at storage setting, or in CrystalDiskInfo as "Health Status" here i have 99% in windows it is also 99%

Here i have all attributes i could use, but it does not contain something "life" or "time" related

I tried calculate it mannualy via summing attributes with coefficients but it didnt lead to good result, so how can i calculate or extract this value ? It should work on different disks regarding manufacturer.

Using Get-PhysicalDisk | Get-StorageReliabilityCounter | Select-Object Wear in PowerShell gives me 0, but it should be 1 at least i guess

Share Improve this question edited Feb 7 at 18:46 Clemens 128k13 gold badges159 silver badges284 bronze badges asked Feb 7 at 17:53 Дмитрий ВитязьДмитрий Витязь 256 bronze badges 3
  • 1 The following may be of interest: CrystalDiskInfo - Github. – It all makes cents Commented Feb 7 at 19:09
  • learn.microsoft.com/en-us/windows/win32/cimwin32prov/… – OldDog Commented Feb 7 at 19:40
  • Could you show the values of the first line of SMART_NVME in the saved text of Crystal Disk Info? – emoacht Commented Feb 8 at 11:27
Add a comment  | 

2 Answers 2

Reset to default 0

Using smartmontools (smartctl), using it as embedded resource calling a command to retrive disk information and extract "Percentage Used" value, at the end to get life we substract our "Percentage Used" value from 100 and we have life left on the disk:

static int GetDiskLifeLeft(string diskPath)
{
    char diskLetter = diskPath[0];
    string arguments = $"-A {diskLetter}:";

    try
    {
        ProcessStartInfo psi = new ProcessStartInfo
        {
            FileName = "smartctl.exe",
            Arguments = arguments,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        using (Process process = new Process { StartInfo = psi })
        {
            process.Start();
            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();
            process.WaitForExit();

            if (!string.IsNullOrWhiteSpace(error))
            {
                return 0;
            }

            string[] lines = output.Split('\n');
            foreach (string line in lines)
            {
                if (line.Contains("Percentage Used", StringComparison.OrdinalIgnoreCase) ||
                    line.Contains("Percentage_Used", StringComparison.OrdinalIgnoreCase))
                {
                    var prc_used = Convert.ToInt16(line.Replace(" ", "").Split(":")[1].Replace("%", ""));
                    return 100 - prc_used;
                }
            }

            return 0;
        }
    }
    catch
    {
        return 0;
    }
}

NVME SMART provides health information including composite temperature, available spare, and percentage used. See the table in NVM Express Base Specification shown below.

CrystalDiskInfo does not show those values as is but you can check the raw values in saved text (File -> Save (text)).

-- SMART_NVME --------------------------------------
     +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F
000: 00 22 01 64 0A 0C 00 00 00 00 00 00 00 00 00 00

In this example, the percentage used is shown at index 5 (0C in hex = 12 in dec) and you can calculate the remaining life subtracting it from 100% (100 - 12 = 88%)

Apart from CrystalDiskInfo, to get SMART information from a strage device directly, you will need to call DeviceIoControl function. But there is a handy library available in GitHub.

usausa/hardwareinfo-disk (HardwareInfo.Disk in NuGet)

Using HardwareInfo.Disk, the information shown in Drive health of Windows 11 can be obtained as follows.

using HardwareInfo.Disk;

public void CheckSmart()
{
    foreach (var disk in DiskInfo.GetInformation())
    {
        Debug.WriteLine($"Index: {disk.Index}");
        Debug.WriteLine($"Model: {disk.Model}");
        Debug.WriteLine($"Smart type: {disk.SmartType}");

        if (disk.Smart is ISmartNvme nvme)
        {
            Debug.WriteLine($"Percentage used: {nvme.PercentageUsed}% -> Estimated remaining life: {100 - nvme.PercentageUsed}%");
            Debug.WriteLine($"Available spare: {nvme.AvailableSpare}");
            Debug.WriteLine($"Temperature: {nvme.Temperature}°C");
        }
    }
}

Please note that you need to run as administrator to get NVME information.

发布评论

评论列表(0)

  1. 暂无评论