LockUtils.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Copyright (C) 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.Text;
  10. using DiskAccessLibrary;
  11. namespace ISCSIConsole
  12. {
  13. public class LockUtils
  14. {
  15. public static void ReleaseDisks(List<Disk> disks)
  16. {
  17. foreach (Disk disk in disks)
  18. {
  19. ReleaseDisk(disk);
  20. }
  21. }
  22. public static void ReleaseDisk(Disk disk)
  23. {
  24. if (disk is DiskImage)
  25. {
  26. ((DiskImage)disk).ReleaseLock();
  27. }
  28. #if Win32
  29. else if (disk is PhysicalDisk)
  30. {
  31. if (!DiskAccessLibrary.LogicalDiskManager.DynamicDisk.IsDynamicDisk(disk))
  32. {
  33. LockHelper.UnlockBasicDiskAndVolumes((PhysicalDisk)disk);
  34. try
  35. {
  36. ((PhysicalDisk)disk).UpdateProperties();
  37. }
  38. catch (System.IO.IOException)
  39. {
  40. }
  41. }
  42. }
  43. else if (disk is VolumeDisk)
  44. {
  45. bool skippedLock = (Environment.OSVersion.Version.Major >= 6 && VolumeInfo.IsOffline(((VolumeDisk)disk).Volume));
  46. if (!skippedLock)
  47. {
  48. Guid? windowsVolumeGuid = WindowsVolumeHelper.GetWindowsVolumeGuid(((VolumeDisk)disk).Volume);
  49. if (windowsVolumeGuid.HasValue)
  50. {
  51. WindowsVolumeManager.ReleaseLock(windowsVolumeGuid.Value);
  52. }
  53. }
  54. }
  55. #endif
  56. }
  57. }
  58. }