PhysicalDiskUtils.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* Copyright (C) 2014-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using Microsoft.Win32.SafeHandles;
  13. using Utilities;
  14. namespace DiskAccessLibrary
  15. {
  16. public enum DeviceType : uint
  17. {
  18. FILE_DEVICE_CD_ROM = 0x00000002,
  19. FILE_DEVICE_DISK = 0x00000007,
  20. }
  21. [StructLayout(LayoutKind.Sequential)]
  22. public struct PARTITION_INFORMATION
  23. {
  24. public long StartingOffset;
  25. public long PartitionLength;
  26. public uint HiddenSectors;
  27. public uint PartitionNumber;
  28. public byte PartitionType;
  29. [MarshalAs(UnmanagedType.I1)]
  30. public bool BootIndicator;
  31. [MarshalAs(UnmanagedType.I1)]
  32. public bool RecognizedPartition;
  33. [MarshalAs(UnmanagedType.I1)]
  34. public bool RewritePartition;
  35. }
  36. [StructLayout(LayoutKind.Sequential)]
  37. public struct DISK_GEOMETRY
  38. {
  39. public long Cylinders;
  40. public uint MediaType;
  41. public uint TracksPerCylinder;
  42. public uint SectorsPerTrack;
  43. public uint BytesPerSector;
  44. }
  45. [StructLayout(LayoutKind.Sequential)]
  46. public struct DISK_GEOMETRY_EX
  47. {
  48. public DISK_GEOMETRY Geometry;
  49. public long DiskSize;
  50. byte Data;
  51. }
  52. [StructLayout(LayoutKind.Sequential)]
  53. public struct STORAGE_DEVICE_NUMBER
  54. {
  55. public uint DeviceType;
  56. public uint DeviceNumber;
  57. public uint PartitionNumber;
  58. }
  59. [StructLayout(LayoutKind.Sequential)]
  60. public struct STORAGE_PROPERTY_QUERY
  61. {
  62. public int PropertyId;
  63. public int QueryType;
  64. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
  65. public byte[] AdditionalParameters;
  66. }
  67. [StructLayout(LayoutKind.Sequential)]
  68. public struct STORAGE_DESCRIPTOR_HEADER
  69. {
  70. public uint Version;
  71. public uint Size;
  72. }
  73. [StructLayout(LayoutKind.Sequential)]
  74. public struct STORAGE_DEVICE_DESCRIPTOR
  75. {
  76. public uint Version;
  77. public uint Size;
  78. public byte DeviceType;
  79. public byte DeviceTypeModifier;
  80. public byte RemovableMedia;
  81. public byte CommandQueueing;
  82. public uint VendorIdOffset;
  83. public uint ProductIdOffset;
  84. public uint ProductRevisionOffset;
  85. public uint SerialNumberOffset;
  86. public byte BusType;
  87. public uint RawPropertiesLength;
  88. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
  89. public byte[] RawDeviceProperties;
  90. }
  91. [StructLayout(LayoutKind.Sequential)]
  92. public struct GET_DISK_ATTRIBUTES
  93. {
  94. public uint Version;
  95. public uint Reserved;
  96. public ulong Attributes;
  97. }
  98. [StructLayout(LayoutKind.Sequential)]
  99. public struct SET_DISK_ATTRIBUTES
  100. {
  101. public uint Version;
  102. [MarshalAs(UnmanagedType.I1)]
  103. public bool Persist;
  104. [MarshalAs(UnmanagedType.I1)]
  105. public bool Reserved1A;
  106. [MarshalAs(UnmanagedType.I1)]
  107. public bool Reserved1B;
  108. [MarshalAs(UnmanagedType.I1)]
  109. public bool Reserved1C;
  110. public ulong Attributes;
  111. public ulong AttributesMask;
  112. public uint Reserved2A;
  113. public uint Reserved2B;
  114. public uint Reserved2C;
  115. public uint Reserved2D;
  116. }
  117. public class PhysicalDiskUtils
  118. {
  119. private const uint IOCTL_DISK_GET_DRIVE_GEOMETRY = 0x00070000;
  120. private const int IOCTL_DISK_GET_DISK_ATTRIBUTES = 0x000700F0;
  121. private const uint IOCTL_DISK_GET_PARTITION_INFO = 0x00074004;
  122. private const uint IOCTL_DISK_GET_DRIVE_GEOMETRY_EX = 0x000700A0; // only supported from XP onward
  123. private const uint IOCTL_DISK_UPDATE_PROPERTIES = 0x00070140;
  124. private const int IOCTL_DISK_SET_DISK_ATTRIBUTES = 0x0007C0F4;
  125. private const int IOCTL_STORAGE_GET_DEVICE_NUMBER = 0x002D1080;
  126. private const int IOCTL_STORAGE_QUERY_PROPERTY = 0x002D1400;
  127. private const int IOCTL_STORAGE_CHECK_VERIFY = 0x002D4800;
  128. private const int IOCTL_STORAGE_CHECK_VERIFY2 = 0x002D0800;
  129. private const uint DISK_ATTRIBUTE_OFFLINE = 0x1;
  130. private const uint DISK_ATTRIBUTE_READ_ONLY = 0x2;
  131. [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)]
  132. public static extern bool DeviceIoControl(SafeHandle hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, IntPtr lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned, IntPtr lpOverlapped);
  133. public static bool IsMediaAccesible(SafeFileHandle hDevice)
  134. {
  135. uint dummy;
  136. bool success = DeviceIoControl(hDevice, IOCTL_STORAGE_CHECK_VERIFY2, IntPtr.Zero, 0,
  137. IntPtr.Zero, 0, out dummy, IntPtr.Zero);
  138. if (!success)
  139. {
  140. int errorCode = Marshal.GetLastWin32Error();
  141. if (errorCode == (int)Win32Error.ERROR_NOT_READY || errorCode == (int)Win32Error.ERROR_NO_MEDIA_IN_DRIVE)
  142. {
  143. return false;
  144. }
  145. else if (errorCode == (int)Win32Error.ERROR_MEDIA_CHANGED)
  146. {
  147. // means that the media has changed. Interpret this error as a success.
  148. // source: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363404%28v=vs.85%29.aspx
  149. return true;
  150. }
  151. else if (errorCode == (int)Win32Error.ERROR_IO_DEVICE || errorCode == (int)Win32Error.ERROR_CRC)
  152. {
  153. // means there is an issue with the media. Interpret this error as a success.
  154. return true;
  155. }
  156. else
  157. {
  158. string message = String.Format("Media is not accessible, Win32 Error: {0}", errorCode);
  159. throw new IOException(message);
  160. }
  161. }
  162. else
  163. {
  164. return true;
  165. }
  166. }
  167. /// <summary>
  168. /// Used to determine the exact disk size on Windows 2000
  169. /// Note: A lot of other programs (including msinfo32) will display the inaccurate
  170. /// size of Cylinders * TracksPerCylinder * SectorsPerTrack * BytesPerSector
  171. /// </summary>
  172. /// <param name="hDevice">Handle to disk or partition</param>
  173. public static long GetPartitionSize(SafeFileHandle hDevice)
  174. {
  175. uint dummy;
  176. PARTITION_INFORMATION partitionInformation = new PARTITION_INFORMATION();
  177. IntPtr lpOutBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(partitionInformation));
  178. Marshal.StructureToPtr(partitionInformation, lpOutBuffer, false);
  179. bool success = DeviceIoControl(hDevice, IOCTL_DISK_GET_PARTITION_INFO, IntPtr.Zero, 0,
  180. lpOutBuffer, (uint)Marshal.SizeOf(typeof(PARTITION_INFORMATION)), out dummy, IntPtr.Zero);
  181. partitionInformation = (PARTITION_INFORMATION)Marshal.PtrToStructure(lpOutBuffer, typeof(PARTITION_INFORMATION));
  182. Marshal.FreeHGlobal(lpOutBuffer);
  183. if (!success)
  184. {
  185. throw new IOException("Unable to retrieve disk geometry");
  186. }
  187. return partitionInformation.PartitionLength;
  188. }
  189. /// <summary>
  190. /// Supported on Windows 2000
  191. /// </summary>
  192. public static DISK_GEOMETRY GetDiskGeometry(SafeFileHandle hDevice)
  193. {
  194. uint dummy;
  195. DISK_GEOMETRY diskGeometry = new DISK_GEOMETRY();
  196. IntPtr lpOutBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(diskGeometry));
  197. Marshal.StructureToPtr(diskGeometry, lpOutBuffer, false);
  198. bool success = DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY, IntPtr.Zero, 0,
  199. lpOutBuffer, (uint)Marshal.SizeOf(typeof(DISK_GEOMETRY)), out dummy, IntPtr.Zero);
  200. diskGeometry = (DISK_GEOMETRY)Marshal.PtrToStructure(lpOutBuffer, typeof(DISK_GEOMETRY));
  201. Marshal.FreeHGlobal(lpOutBuffer);
  202. if (!success)
  203. {
  204. throw new IOException("Unable to retrieve disk geometry");
  205. }
  206. return diskGeometry;
  207. }
  208. /// <summary>
  209. /// Supported on Windows XP and upward
  210. /// </summary>
  211. public static DISK_GEOMETRY_EX GetDiskGeometryEx(SafeFileHandle hDevice)
  212. {
  213. uint dummy;
  214. DISK_GEOMETRY_EX diskGeometryEx = new DISK_GEOMETRY_EX();
  215. IntPtr lpOutBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(diskGeometryEx));
  216. Marshal.StructureToPtr(diskGeometryEx, lpOutBuffer, false);
  217. bool success = DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, IntPtr.Zero, 0,
  218. lpOutBuffer, (uint)Marshal.SizeOf(typeof(DISK_GEOMETRY_EX)), out dummy, IntPtr.Zero);
  219. diskGeometryEx = (DISK_GEOMETRY_EX)Marshal.PtrToStructure(lpOutBuffer, typeof(DISK_GEOMETRY_EX));
  220. Marshal.FreeHGlobal(lpOutBuffer);
  221. if (!success)
  222. {
  223. int errorCode = Marshal.GetLastWin32Error();
  224. if (errorCode == (int)Win32Error.ERROR_INVALID_FUNCTION)
  225. {
  226. // Windows 2000 will throw this exception because IOCTL_DISK_GET_DRIVE_GEOMETRY_EX is only supported from XP onward
  227. throw new InvalidOperationException("IOCTL_DISK_GET_DRIVE_GEOMETRY_EX is not supported");
  228. }
  229. else
  230. {
  231. string message = String.Format("Unable to retrieve disk geometry, Win32 Error: {0}", errorCode);
  232. throw new IOException(message);
  233. }
  234. }
  235. return diskGeometryEx;
  236. }
  237. public static DISK_GEOMETRY GetDiskGeometryAndSize(SafeFileHandle hDevice, out long diskSize)
  238. {
  239. if (Environment.OSVersion.Version >= new Version(5, 1, 0, 0))
  240. {
  241. // XP and upward
  242. DISK_GEOMETRY_EX diskGeometryEx = GetDiskGeometryEx(hDevice);
  243. diskSize = diskGeometryEx.DiskSize;
  244. return diskGeometryEx.Geometry;
  245. }
  246. else
  247. {
  248. // Windows 2000
  249. DISK_GEOMETRY diskGeometry = GetDiskGeometry(hDevice);
  250. diskSize = GetPartitionSize(hDevice);
  251. return diskGeometry;
  252. }
  253. }
  254. public static long GetDiskSize(SafeFileHandle hDevice)
  255. {
  256. long size;
  257. GetDiskGeometryAndSize(hDevice, out size);
  258. return size;
  259. }
  260. public static int GetBytesPerSector(SafeFileHandle hDevice)
  261. {
  262. long size;
  263. DISK_GEOMETRY diskGeometry = GetDiskGeometryAndSize(hDevice, out size);
  264. return (int)diskGeometry.BytesPerSector;
  265. }
  266. /// <summary>
  267. /// Invalidates the cached partition table and re-enumerates the device
  268. /// </summary>
  269. public static bool UpdateDiskProperties(SafeFileHandle hDevice)
  270. {
  271. uint dummy;
  272. bool success = DeviceIoControl(hDevice, IOCTL_DISK_UPDATE_PROPERTIES, IntPtr.Zero, 0,
  273. IntPtr.Zero, 0, out dummy, IntPtr.Zero);
  274. return success;
  275. }
  276. public static STORAGE_DEVICE_NUMBER GetDeviceNumber(SafeFileHandle hDevice)
  277. {
  278. uint dummy;
  279. STORAGE_DEVICE_NUMBER storageDeviceNumber = new STORAGE_DEVICE_NUMBER();
  280. IntPtr lpOutBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(storageDeviceNumber));
  281. bool success = DeviceIoControl(hDevice, IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero, 0,
  282. lpOutBuffer, (uint)Marshal.SizeOf(typeof(STORAGE_DEVICE_NUMBER)), out dummy, IntPtr.Zero);
  283. storageDeviceNumber = (STORAGE_DEVICE_NUMBER)Marshal.PtrToStructure(lpOutBuffer, typeof(STORAGE_DEVICE_NUMBER));
  284. Marshal.FreeHGlobal(lpOutBuffer);
  285. if (!success)
  286. {
  287. throw new IOException("Unable to retrieve device number");
  288. }
  289. return storageDeviceNumber;
  290. }
  291. public static List<int> GetPhysicalDiskIndexList()
  292. {
  293. List<string> devicePathList = DeviceInterfaceUtils.GetDevicePathList(DeviceInterfaceUtils.DiskClassGuid);
  294. List<int> result = new List<int>();
  295. foreach (string devicePath in devicePathList)
  296. {
  297. SafeFileHandle hDevice = HandleUtils.GetFileHandle(devicePath, FileAccess.Read, ShareMode.ReadWrite);
  298. STORAGE_DEVICE_NUMBER number = GetDeviceNumber(hDevice);
  299. hDevice.Close();
  300. result.Add((int)number.DeviceNumber);
  301. }
  302. // We'll now sort the list based on disk number
  303. result.Sort();
  304. return result;
  305. }
  306. /// <summary>
  307. /// Returns the device product ID
  308. /// </summary>
  309. public static string GetDeviceDescription(SafeFileHandle hDevice)
  310. {
  311. STORAGE_DEVICE_DESCRIPTOR deviceDescriptor = GetDeviceDescriptor(hDevice);
  312. string description = String.Empty;
  313. if (deviceDescriptor.VendorIdOffset > 0)
  314. {
  315. int offset = (int)deviceDescriptor.VendorIdOffset - Marshal.SizeOf(deviceDescriptor);
  316. string vendor = ByteReader.ReadNullTerminatedAnsiString(deviceDescriptor.RawDeviceProperties, offset);
  317. while (vendor.EndsWith(" ")) // Remove multiple empty space
  318. {
  319. vendor = vendor.Remove(vendor.Length - 1);
  320. }
  321. description += vendor;
  322. }
  323. if (deviceDescriptor.ProductIdOffset > 0)
  324. {
  325. int offset = (int)deviceDescriptor.ProductIdOffset - Marshal.SizeOf(deviceDescriptor);
  326. string product = ByteReader.ReadNullTerminatedAnsiString(deviceDescriptor.RawDeviceProperties, offset);
  327. description += product.Trim();
  328. }
  329. return description;
  330. }
  331. /// <summary>
  332. /// Returns the device serial number
  333. /// </summary>
  334. public static string GetDeviceSerialNumber(SafeFileHandle hDevice)
  335. {
  336. STORAGE_DEVICE_DESCRIPTOR deviceDescriptor = GetDeviceDescriptor(hDevice);
  337. // SerialNumberOffset = 0xFFFFFFFF means the device have no serial number
  338. if (deviceDescriptor.SerialNumberOffset > 0 && deviceDescriptor.SerialNumberOffset != 0xFFFFFFFF)
  339. {
  340. int offset = (int)deviceDescriptor.SerialNumberOffset - Marshal.SizeOf(deviceDescriptor);
  341. string serialNumber = ByteReader.ReadNullTerminatedAnsiString(deviceDescriptor.RawDeviceProperties, offset);
  342. return serialNumber.Trim();
  343. }
  344. return String.Empty;
  345. }
  346. public static STORAGE_DEVICE_DESCRIPTOR GetDeviceDescriptor(SafeFileHandle hDevice)
  347. {
  348. const int StorageDeviceProperty = 0;
  349. const int PropertyStandardQuery = 0;
  350. uint dummy;
  351. STORAGE_PROPERTY_QUERY storagePropertyQuery = new STORAGE_PROPERTY_QUERY();
  352. storagePropertyQuery.PropertyId = StorageDeviceProperty;
  353. storagePropertyQuery.QueryType = PropertyStandardQuery;
  354. IntPtr lpInBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(storagePropertyQuery));
  355. Marshal.StructureToPtr(storagePropertyQuery, lpInBuffer, false);
  356. STORAGE_DESCRIPTOR_HEADER storageDescriptorHeader = new STORAGE_DESCRIPTOR_HEADER();
  357. IntPtr lpOutBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(storageDescriptorHeader));
  358. Marshal.StructureToPtr(storageDescriptorHeader, lpOutBuffer, false);
  359. bool success = DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, lpInBuffer, (uint)Marshal.SizeOf(storagePropertyQuery),
  360. lpOutBuffer, (uint)Marshal.SizeOf(storageDescriptorHeader), out dummy, IntPtr.Zero);
  361. if (!success)
  362. {
  363. int errorCode = Marshal.GetLastWin32Error();
  364. throw new IOException("Unable to retrieve device description header, Error: " + errorCode.ToString());
  365. }
  366. storageDescriptorHeader = (STORAGE_DESCRIPTOR_HEADER)Marshal.PtrToStructure(lpOutBuffer, typeof(STORAGE_DESCRIPTOR_HEADER));
  367. Marshal.FreeHGlobal(lpOutBuffer);
  368. lpOutBuffer = Marshal.AllocHGlobal((int)storageDescriptorHeader.Size);
  369. success = DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, lpInBuffer, (uint)Marshal.SizeOf(storagePropertyQuery),
  370. lpOutBuffer, storageDescriptorHeader.Size, out dummy, IntPtr.Zero);
  371. if (!success)
  372. {
  373. int errorCode = Marshal.GetLastWin32Error();
  374. throw new IOException("Unable to retrieve device description, Error: " + errorCode.ToString());
  375. }
  376. STORAGE_DEVICE_DESCRIPTOR deviceDescriptor = (STORAGE_DEVICE_DESCRIPTOR)Marshal.PtrToStructure(lpOutBuffer, typeof(STORAGE_DEVICE_DESCRIPTOR));
  377. int rawDevicePropertiesOffset = Marshal.SizeOf(deviceDescriptor);
  378. int rawDevicePropertiesLength = (int)storageDescriptorHeader.Size - rawDevicePropertiesOffset;
  379. deviceDescriptor.RawDeviceProperties = new byte[rawDevicePropertiesLength];
  380. Marshal.Copy(new IntPtr(lpOutBuffer.ToInt64() + rawDevicePropertiesOffset), deviceDescriptor.RawDeviceProperties, 0, rawDevicePropertiesLength);
  381. Marshal.FreeHGlobal(lpOutBuffer);
  382. Marshal.FreeHGlobal(lpInBuffer);
  383. return deviceDescriptor;
  384. }
  385. /// <summary>
  386. /// Available on Windows Vista and newer
  387. /// </summary>
  388. public static bool GetOnlineStatus(SafeFileHandle hDevice)
  389. {
  390. bool isReadOnly;
  391. return GetOnlineStatus(hDevice, out isReadOnly);
  392. }
  393. /// <summary>
  394. /// Available on Windows Vista and newer
  395. /// </summary>
  396. public static bool GetOnlineStatus(SafeFileHandle hDevice, out bool isReadOnly)
  397. {
  398. uint dummy;
  399. GET_DISK_ATTRIBUTES attributes = new GET_DISK_ATTRIBUTES();
  400. IntPtr lpOutBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(attributes));
  401. bool success = DeviceIoControl(hDevice, IOCTL_DISK_GET_DISK_ATTRIBUTES, IntPtr.Zero, 0,
  402. lpOutBuffer, (uint)Marshal.SizeOf(typeof(GET_DISK_ATTRIBUTES)), out dummy, IntPtr.Zero);
  403. attributes = (GET_DISK_ATTRIBUTES)Marshal.PtrToStructure(lpOutBuffer, typeof(GET_DISK_ATTRIBUTES));
  404. Marshal.FreeHGlobal(lpOutBuffer);
  405. if (!success)
  406. {
  407. int errorCode = Marshal.GetLastWin32Error();
  408. throw new IOException("Unable to retrieve disk attributes, Error: " + errorCode.ToString());
  409. }
  410. bool isOffline = (attributes.Attributes & DISK_ATTRIBUTE_OFFLINE) > 0;
  411. isReadOnly = (attributes.Attributes & DISK_ATTRIBUTE_READ_ONLY) > 0;
  412. return !isOffline;
  413. }
  414. /// <summary>
  415. /// Take the disk offline / online, Available on Windows Vista and newer
  416. /// </summary>
  417. /// <param name="persist">persist is effective for both online==true and online==false</param>
  418. private static void TrySetOnlineStatus(SafeFileHandle hDevice, bool online, bool persist)
  419. {
  420. uint dummy;
  421. SET_DISK_ATTRIBUTES attributes = new SET_DISK_ATTRIBUTES();
  422. attributes.Version = (uint)Marshal.SizeOf(attributes);
  423. attributes.Attributes = online ? 0 : DISK_ATTRIBUTE_OFFLINE;
  424. attributes.AttributesMask = DISK_ATTRIBUTE_OFFLINE;
  425. attributes.Persist = persist;
  426. IntPtr lpInBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(attributes));
  427. Marshal.StructureToPtr(attributes, lpInBuffer, true);
  428. bool success = DeviceIoControl(hDevice, IOCTL_DISK_SET_DISK_ATTRIBUTES, lpInBuffer, (uint)Marshal.SizeOf(typeof(SET_DISK_ATTRIBUTES)),
  429. IntPtr.Zero, 0, out dummy, IntPtr.Zero);
  430. Marshal.FreeHGlobal(lpInBuffer);
  431. if (!success)
  432. {
  433. int errorCode = Marshal.GetLastWin32Error();
  434. throw new IOException("Unable to set disk attributes, Error: " + errorCode.ToString());
  435. }
  436. }
  437. /// <summary>
  438. /// Available on Windows Vista and newer
  439. /// </summary>
  440. public static bool SetOnlineStatus(SafeFileHandle hDevice, bool online, bool persist)
  441. {
  442. if (GetOnlineStatus(hDevice) != online)
  443. {
  444. TrySetOnlineStatus(hDevice, online, persist);
  445. return (GetOnlineStatus(hDevice) == online);
  446. }
  447. return true;
  448. }
  449. }
  450. }