这不是100%万无一失,但它是Linux使用的方法。 它会检查“SSD”一词是否在驱动器的产品ID中:
#include <tchar.h> #include <windows.h> #include <winioctl.h> void Check(LPCTSTR file, LPCTSTR volPathWithLeadingPrefix) { HANDLE hFile = CreateFile( volPathWithLeadingPrefix, FILE_READ_ATTRIBUTES, // Just querying; don't mount FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL); STORAGE_PROPERTY_QUERY spq = { StorageDeviceProperty, PropertyStandardQuery }; union { STORAGE_DEVICE_DESCRIPTOR StorageDeviceDescriptor; BYTE Data[1024]; } output = {0}; DWORD br; if (DeviceIoControl( hFile, IOCTL_STORAGE_QUERY_PROPERTY, &spq, sizeof(spq), &output, sizeof(output), &br, NULL)) { STORAGE_DEVICE_DESCRIPTOR &sdd = output.StorageDeviceDescriptor; if (sdd.ProductIdOffset > 0) { LPCSTR productID = (LPCSTR)&((LPBYTE)&sdd)[sdd.ProductIdOffset]; BOOL isSSD = strstr(productID, "SSD") != NULL; _tprintf(_T("\"%s\": %s\n"), file, isSSD ? _T("SSD") : _T("HDD")); } else { _ftprintf(stderr, _T("No product ID.")); } } else { _ftprintf(stderr, _T("Error %u querying storage.\n"), GetLastError()); } } int _tmain(int argc, TCHAR *argv[]) { for (int i = 1; i < argc; i++) { LPCTSTR file = argv[i]; TCHAR volPath[MAX_PATH]; if (GetVolumePathName(file, volPath, ARRAYSIZE(volPath))) { for (size_t cchVolPath = _tcslen(volPath); cchVolPath > 0 && volPath[cchVolPath - 1] == TEXT('\\'); cchVolPath--) { volPath[cchVolPath - 1] = TEXT('\0'); } TCHAR volPathWithLeadingPrefix[ARRAYSIZE(volPath)]; if (_sntprintf( volPathWithLeadingPrefix, ARRAYSIZE(volPathWithLeadingPrefix), volPath[0] == _T('\\') ? _T("%s") : _T("\\\\.\\%s"), volPath) < ARRAYSIZE(volPathWithLeadingPrefix)) { Check(file, volPathWithLeadingPrefix); } else { SetLastError(ERROR_FILENAME_EXCED_RANGE); _ftprintf(stderr, _T("Path \"%s\" is too long.\n"), volPath); } } else { _ftprintf( stderr, _T("Error %u getting volume path.\n"), GetLastError()); } } }