VolumeControl.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (C) 2014 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. namespace DiskAccessLibrary
  14. {
  15. public class VolumeControl
  16. {
  17. private const uint FSCTL_IS_VOLUME_MOUNTED = 0x90028;
  18. private const uint FSCTL_DISMOUNT_VOLUME = 0x90020;
  19. private const uint FSCTL_LOCK_VOLUME = 0x90018;
  20. private const uint FSCTL_UNLOCK_VOLUME = 0x9001C;
  21. private const uint FSCTL_ALLOW_EXTENDED_DASD_IO = 0x90083;
  22. public const int MaxPath = 260;
  23. [DllImport("kernel32.dll", SetLastError = true)]
  24. private static extern bool GetVolumePathNamesForVolumeNameW(
  25. [MarshalAs(UnmanagedType.LPWStr)]
  26. string lpszVolumeName,
  27. [MarshalAs(UnmanagedType.LPWStr)]
  28. string lpszVolumePathNames,
  29. uint cchBuferLength,
  30. ref uint lpcchReturnLength);
  31. /// <summary>
  32. /// e.g.
  33. /// D:\
  34. /// E:\MountPoint\
  35. /// </summary>
  36. public static List<string> GetVolumeMountPoints(Guid volumeGuid)
  37. {
  38. string volumeGuidPath = String.Format(@"\\?\Volume{0}\", volumeGuid.ToString("B"));
  39. string lpszVolumeName = new string(new char[MaxPath]);
  40. uint returnLength = 0;
  41. bool success = GetVolumePathNamesForVolumeNameW(volumeGuidPath, lpszVolumeName, (uint)lpszVolumeName.Length, ref returnLength);
  42. if (success)
  43. {
  44. lpszVolumeName = lpszVolumeName.Substring(0, (int)returnLength);
  45. }
  46. else
  47. {
  48. if (Marshal.GetLastWin32Error() == (int)Win32Error.ERROR_MORE_DATA)
  49. {
  50. lpszVolumeName = new string(new char[returnLength]);
  51. success = GetVolumePathNamesForVolumeNameW(volumeGuidPath, lpszVolumeName, (uint)lpszVolumeName.Length, ref returnLength);
  52. }
  53. }
  54. if (success)
  55. {
  56. lpszVolumeName = lpszVolumeName.TrimEnd('\0');
  57. if (lpszVolumeName != String.Empty)
  58. {
  59. List<string> result = new List<string>(lpszVolumeName.Split('\0'));
  60. return result;
  61. }
  62. }
  63. return new List<string>();
  64. }
  65. public static bool IsVolumeMounted(char driveLetter)
  66. {
  67. SafeFileHandle handle = HandleUtils.GetVolumeHandle(driveLetter, FileAccess.Read, ShareMode.ReadWrite);
  68. return IsVolumeMounted(handle);
  69. }
  70. public static bool IsVolumeMounted(string path)
  71. {
  72. SafeFileHandle handle = HandleUtils.GetVolumeHandle(path, FileAccess.Read, ShareMode.ReadWrite);
  73. return IsVolumeMounted(handle);
  74. }
  75. public static bool IsVolumeMounted(Guid volumeGuid)
  76. {
  77. SafeFileHandle handle = HandleUtils.GetVolumeHandle(volumeGuid, FileAccess.Read, ShareMode.ReadWrite);
  78. return IsVolumeMounted(handle);
  79. }
  80. /// <param name="handle">When opening a volume, the dwShareMode parameter must have the FILE_SHARE_WRITE flag.</param>
  81. public static bool IsVolumeMounted(SafeFileHandle handle)
  82. {
  83. if (!handle.IsInvalid)
  84. {
  85. uint dummy;
  86. bool mounted = PhysicalDiskControl.DeviceIoControl(handle, FSCTL_IS_VOLUME_MOUNTED, IntPtr.Zero, 0, IntPtr.Zero, 0, out dummy, IntPtr.Zero);
  87. handle.Close();
  88. return mounted;
  89. }
  90. else
  91. {
  92. return false;
  93. }
  94. }
  95. // By locking the volume before you dismount it, you can ensure that the volume is dismounted cleanly
  96. // (because the system flushes all cached data to the volume before locking it)
  97. //
  98. // A locked volume remains locked until one of the following occurs:
  99. // * The application uses the FSCTL_UNLOCK_VOLUME control code to unlock the volume.
  100. // * The handle closes, either directly through CloseHandle, or indirectly when a process terminates.
  101. // http://msdn.microsoft.com/en-us/library/bb521494(v=winembedded.5).aspx
  102. // http://msdn.microsoft.com/en-us/library/Aa364575
  103. /// <param name="handle">
  104. /// The application must specify the FILE_SHARE_READ and FILE_SHARE_WRITE flags in the dwShareMode parameter of CreateFile.
  105. /// https://msdn.microsoft.com/en-us/library/Aa364575
  106. /// </param>
  107. public static bool LockVolume(SafeFileHandle handle)
  108. {
  109. if (!handle.IsInvalid)
  110. {
  111. uint dummy;
  112. bool success = PhysicalDiskControl.DeviceIoControl(handle, FSCTL_LOCK_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, out dummy, IntPtr.Zero);
  113. return success;
  114. }
  115. else
  116. {
  117. return false;
  118. }
  119. }
  120. // Forced dismount only does FSCTL_DISMOUNT_VOLUME (without locking the volume first),
  121. // and then does the formatting write/verify operations _from this very handle_.
  122. // Then the handle is just closed, and any next attempt to access the volume will automount it.
  123. /// <param name="handle">
  124. /// The application must specify the FILE_SHARE_READ and FILE_SHARE_WRITE flags in the dwShareMode parameter of CreateFile.
  125. /// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364562%28v=vs.85%29.aspx
  126. /// </param>
  127. public static bool DismountVolume(SafeFileHandle handle)
  128. {
  129. if (!handle.IsInvalid)
  130. {
  131. uint dummy;
  132. bool success = PhysicalDiskControl.DeviceIoControl(handle, FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, out dummy, IntPtr.Zero);
  133. if (!success)
  134. {
  135. int errorCode = Marshal.GetLastWin32Error();
  136. if (errorCode == (int)Win32Error.ERROR_ACCESS_DENIED)
  137. {
  138. throw new UnauthorizedAccessException();
  139. }
  140. }
  141. return success;
  142. }
  143. else
  144. {
  145. return false;
  146. }
  147. }
  148. public static bool AllowExtendedIO(SafeFileHandle handle)
  149. {
  150. if (!handle.IsInvalid)
  151. {
  152. uint dummy;
  153. bool success = PhysicalDiskControl.DeviceIoControl(handle, FSCTL_ALLOW_EXTENDED_DASD_IO, IntPtr.Zero, 0, IntPtr.Zero, 0, out dummy, IntPtr.Zero);
  154. if (!success)
  155. {
  156. int errorCode = Marshal.GetLastWin32Error();
  157. if (errorCode == (int)Win32Error.ERROR_ACCESS_DENIED)
  158. {
  159. throw new UnauthorizedAccessException();
  160. }
  161. }
  162. return success;
  163. }
  164. else
  165. {
  166. return false;
  167. }
  168. }
  169. }
  170. }