WindowsDynamicVolumeHelper.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.Text;
  10. using DiskAccessLibrary.LogicalDiskManager;
  11. using Utilities;
  12. namespace DiskAccessLibrary
  13. {
  14. public class WindowsDynamicVolumeHelper
  15. {
  16. public static List<DynamicVolume> GetDynamicVolumes()
  17. {
  18. List<DynamicDisk> disks = WindowsDynamicDiskHelper.GetPhysicalDynamicDisks();
  19. return DynamicVolumeHelper.GetDynamicVolumes(disks);
  20. }
  21. public static List<DynamicVolume> GetLockableDynamicVolumes(List<DynamicDisk> dynamicDisks)
  22. {
  23. List<DynamicVolume> result = new List<DynamicVolume>();
  24. List<DynamicDisk> disks = new List<DynamicDisk>();
  25. foreach (DynamicDisk dynamicDisk in dynamicDisks)
  26. {
  27. if (dynamicDisk.Disk is PhysicalDisk)
  28. {
  29. disks.Add(dynamicDisk);
  30. }
  31. }
  32. return DynamicVolumeHelper.GetDynamicVolumes(disks);
  33. }
  34. public static bool LockAllMountedOrNone(List<DynamicVolume> volumes)
  35. {
  36. bool success = true;
  37. int lockIndex;
  38. for (lockIndex = 0; lockIndex < volumes.Count; lockIndex++)
  39. {
  40. // NOTE: The fact that a volume does not have mount points, does not mean it is not mounted and cannot be accessed by Windows
  41. success = WindowsVolumeManager.ExclusiveLockIfMounted(volumes[lockIndex].VolumeGuid);
  42. if (!success)
  43. {
  44. break;
  45. }
  46. }
  47. if (!success)
  48. {
  49. // release the volumes that were locked
  50. for (int index = 0; index < lockIndex; index++)
  51. {
  52. WindowsVolumeManager.ReleaseLock(volumes[index].VolumeGuid);
  53. }
  54. }
  55. return success;
  56. }
  57. }
  58. }