WindowsDynamicDiskHelper.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.Text;
  11. using DiskAccessLibrary.LogicalDiskManager;
  12. using Utilities;
  13. namespace DiskAccessLibrary
  14. {
  15. public class WindowsDynamicDiskHelper
  16. {
  17. public static List<DynamicDisk> GetPhysicalDynamicDisks()
  18. {
  19. List<PhysicalDisk> disks = PhysicalDiskHelper.GetPhysicalDisks();
  20. List<DynamicDisk> result = new List<DynamicDisk>();
  21. foreach (PhysicalDisk disk in disks)
  22. {
  23. DynamicDisk dynamicDisk = DynamicDisk.ReadFromDisk(disk);
  24. if (dynamicDisk != null)
  25. {
  26. result.Add(dynamicDisk);
  27. }
  28. }
  29. return result;
  30. }
  31. public static PrivateHeader FindDiskPrivateHeader(Guid diskGuid)
  32. {
  33. DynamicDisk disk = FindDisk(diskGuid);
  34. if (disk != null)
  35. {
  36. return disk.PrivateHeader;
  37. }
  38. return null;
  39. }
  40. public static DynamicDisk FindDisk(Guid diskGuid)
  41. {
  42. List<int> diskIndexList = PhysicalDiskUtils.GetPhysicalDiskIndexList();
  43. foreach (int diskIndex in diskIndexList)
  44. {
  45. PhysicalDisk disk;
  46. try
  47. {
  48. disk = new PhysicalDisk(diskIndex); // will throw an exception if disk is not valid
  49. }
  50. catch (DriveNotFoundException)
  51. {
  52. // The disk must have been removed from the system
  53. continue;
  54. }
  55. catch (DeviceNotReadyException)
  56. {
  57. continue;
  58. }
  59. catch (SharingViolationException) // skip this disk, it's probably being used
  60. {
  61. continue;
  62. }
  63. DynamicDisk dynamicDisk = DynamicDisk.ReadFromDisk(disk);
  64. if (dynamicDisk != null)
  65. {
  66. if (dynamicDisk.DiskGuid == diskGuid)
  67. {
  68. return dynamicDisk;
  69. }
  70. }
  71. }
  72. return null;
  73. }
  74. }
  75. }