Exemplo n.º 1
0
 // By locking the volume before you dismount it, you can ensure that the volume is dismounted cleanly
 // (because the system flushes all cached data to the volume before locking it)
 //
 // A locked volume remains locked until one of the following occurs:
 // * The application uses the FSCTL_UNLOCK_VOLUME control code to unlock the volume.
 // * The handle closes, either directly through CloseHandle, or indirectly when a process terminates.
 // http://msdn.microsoft.com/en-us/library/bb521494(v=winembedded.5).aspx
 // http://msdn.microsoft.com/en-us/library/Aa364575
 /// <param name="handle">
 /// The application must specify the FILE_SHARE_READ and FILE_SHARE_WRITE flags in the dwShareMode parameter of CreateFile.
 /// https://msdn.microsoft.com/en-us/library/Aa364575
 /// </param>
 public static bool LockVolume(SafeFileHandle handle)
 {
     if (!handle.IsInvalid)
     {
         uint dummy;
         bool success = PhysicalDiskControl.DeviceIoControl(handle, FSCTL_LOCK_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, out dummy, IntPtr.Zero);
         return(success);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Note: The NTFS file system treats a locked volume as a dismounted volume.
 /// </summary>
 /// <param name="handle">When opening a volume, the dwShareMode parameter must have the FILE_SHARE_WRITE flag.</param>
 public static bool IsVolumeMounted(SafeFileHandle handle)
 {
     if (!handle.IsInvalid)
     {
         uint dummy;
         bool mounted = PhysicalDiskControl.DeviceIoControl(handle, FSCTL_IS_VOLUME_MOUNTED, IntPtr.Zero, 0, IntPtr.Zero, 0, out dummy, IntPtr.Zero);
         handle.Close();
         return(mounted);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 3
0
 public static bool AllowExtendedIO(SafeFileHandle handle)
 {
     if (!handle.IsInvalid)
     {
         uint dummy;
         bool success = PhysicalDiskControl.DeviceIoControl(handle, FSCTL_ALLOW_EXTENDED_DASD_IO, IntPtr.Zero, 0, IntPtr.Zero, 0, out dummy, IntPtr.Zero);
         if (!success)
         {
             int errorCode = Marshal.GetLastWin32Error();
             if (errorCode == (int)Win32Error.ERROR_ACCESS_DENIED)
             {
                 throw new UnauthorizedAccessException();
             }
         }
         return(success);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 4
0
 // Forced dismount only does FSCTL_DISMOUNT_VOLUME (without locking the volume first),
 // and then does the formatting write/verify operations _from this very handle_.
 // Then the handle is just closed, and any next attempt to access the volume will automount it.
 /// <param name="handle">
 /// The application must specify the FILE_SHARE_READ and FILE_SHARE_WRITE flags in the dwShareMode parameter of CreateFile.
 /// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364562%28v=vs.85%29.aspx
 /// </param>
 public static bool DismountVolume(SafeFileHandle handle)
 {
     if (!handle.IsInvalid)
     {
         uint dummy;
         bool success = PhysicalDiskControl.DeviceIoControl(handle, FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, out dummy, IntPtr.Zero);
         if (!success)
         {
             int errorCode = Marshal.GetLastWin32Error();
             if (errorCode == (int)Win32Error.ERROR_ACCESS_DENIED)
             {
                 throw new UnauthorizedAccessException();
             }
         }
         return(success);
     }
     else
     {
         return(false);
     }
 }