What is the easiest way in C# to check if hard disk is SSD without writing any file on hard disk?

前端 未结 2 2016
庸人自扰
庸人自扰 2020-12-31 06:21

I need to check in C# if a hard disk is SSD (Solid-state drive), no seek penalty? I used:

    ManagementClass driveClass = new ManagementClass(\"Win32_DiskDr         


        
2条回答
  •  天涯浪人
    2020-12-31 06:24

    This will give you the result on Win10

    ManagementScope scope = new ManagementScope(@"\\.\root\microsoft\windows\storage");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM MSFT_PhysicalDisk");
    string type = "";
    scope.Connect();
    searcher.Scope = scope;
    
    foreach (ManagementObject queryObj in searcher.Get())
    {       
        switch (Convert.ToInt16(queryObj["MediaType"]))
        {
            case 1:
                type = "Unspecified";
                break;
    
            case 3:
                type = "HDD";
                break;
    
            case 4:
                type = "SSD";
                break;
    
            case 5:
                type = "SCM";
                break;
    
            default:
                type = "Unspecified";
                break;
        }
    }
    searcher.Dispose();
    

    P.s. the string type is the last drive, change to an array to get it for all drives

提交回复
热议问题