private static uint GetDiskExtents(char cDrive) { if (new DriveInfo(cDrive.ToString((IFormatProvider)CultureInfo.InvariantCulture)).DriveType != DriveType.Fixed) { Logger.Info(string.Format("The drive {0} is not fixed drive.", (object)cDrive)); } string lpFileName = "\\\\.\\" + cDrive.ToString((IFormatProvider)CultureInfo.InvariantCulture) + ":"; SafeFileHandle fileW = SSDCheck.CreateFileW(lpFileName, 0U, 3U, IntPtr.Zero, 3U, 128U, IntPtr.Zero); if (fileW == null || fileW.IsInvalid) { string errorMessage = SSDCheck.GetErrorMessage(Marshal.GetLastWin32Error()); Logger.Error("CreateFile failed for " + lpFileName + ". " + errorMessage); throw new Exception("check the error message above"); } uint dwIoControlCode = SSDCheck.CTL_CODE(86U, 0U, 0U, 0U); SSDCheck.VOLUME_DISK_EXTENTS lpOutBuffer = new SSDCheck.VOLUME_DISK_EXTENTS(); int num = SSDCheck.DeviceIoControl(fileW, dwIoControlCode, IntPtr.Zero, 0U, ref lpOutBuffer, (uint)Marshal.SizeOf((object)lpOutBuffer), out uint _, IntPtr.Zero) ? 1 : 0; fileW?.Close(); if (num == 0 || lpOutBuffer.Extents.Length != 1) { Logger.Error("DeviceIoControl failed to query disk extension. " + SSDCheck.GetErrorMessage(Marshal.GetLastWin32Error())); throw new Exception("check the error message above"); } uint diskNumber = lpOutBuffer.Extents[0].DiskNumber; Logger.Info(string.Format("The physical drive number is: {0}", (object)diskNumber)); return(diskNumber); }
private static bool HasTrimEnabled(string drive) { Logger.Info("Checking trim enabled for drive: " + drive); SafeFileHandle fileW = SSDCheck.CreateFileW(drive, 0U, 3U, IntPtr.Zero, 3U, 128U, IntPtr.Zero); if (fileW == null || fileW.IsInvalid) { Logger.Error("CreateFile failed with message: " + SSDCheck.GetErrorMessage(Marshal.GetLastWin32Error())); throw new Exception("check the error message above"); } uint dwIoControlCode = SSDCheck.CTL_CODE(45U, 1280U, 0U, 0U); SSDCheck.STORAGE_PROPERTY_QUERY lpInBuffer = new SSDCheck.STORAGE_PROPERTY_QUERY() { PropertyId = 8, QueryType = 0 }; SSDCheck.DEVICE_TRIM_DESCRIPTOR lpOutBuffer = new SSDCheck.DEVICE_TRIM_DESCRIPTOR(); int num = SSDCheck.DeviceIoControl(fileW, dwIoControlCode, ref lpInBuffer, (uint)Marshal.SizeOf((object)lpInBuffer), ref lpOutBuffer, (uint)Marshal.SizeOf((object)lpOutBuffer), out uint _, IntPtr.Zero) ? 1 : 0; fileW?.Close(); if (num == 0) { Logger.Error("DeviceIoControl failed to query trim enabled. " + SSDCheck.GetErrorMessage(Marshal.GetLastWin32Error())); throw new Exception("check the error message above"); } bool trimEnabled = lpOutBuffer.TrimEnabled; Logger.Info(string.Format("Is Trim Enabled: {0}", (object)trimEnabled)); return(trimEnabled); }