WindowsDynamicDiskHelper.cs 2.6 KB

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