WindowsVolumeHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 Microsoft.Win32;
  11. using DiskAccessLibrary.LogicalDiskManager;
  12. using Utilities;
  13. namespace DiskAccessLibrary
  14. {
  15. public class WindowsVolumeHelper
  16. {
  17. /// <summary>
  18. /// Get Windows Volume Guid of MBR disk
  19. /// </summary>
  20. /// <param name="partitionStartOffset">In bytes</param>
  21. /// <returns></returns>
  22. public static Guid? GetWindowsVolumeGuid(uint mbrDiskSignature, ulong partitionStartOffset)
  23. {
  24. byte[] identifier = new byte[12];
  25. LittleEndianWriter.WriteUInt32(identifier, 0, mbrDiskSignature);
  26. LittleEndianWriter.WriteUInt64(identifier, 4, partitionStartOffset);
  27. RegistryKey mountedDevices = Registry.LocalMachine.OpenSubKey(@"SYSTEM\MountedDevices");
  28. foreach (string valueName in mountedDevices.GetValueNames())
  29. {
  30. object valueObject = mountedDevices.GetValue(valueName);
  31. byte[] value = valueObject as byte[];
  32. if (value != null && value.Length == 12)
  33. {
  34. if (ByteUtils.AreByteArraysEqual(value, identifier))
  35. {
  36. if (valueName.StartsWith(@"\??\Volume"))
  37. {
  38. string guidString = valueName.Substring(10);
  39. return new Guid(guidString);
  40. }
  41. }
  42. }
  43. }
  44. return null;
  45. }
  46. public static Guid? GetWindowsVolumeGuid(Volume volume)
  47. {
  48. if (volume is MBRPartition)
  49. {
  50. MBRPartition partition = (MBRPartition)volume;
  51. MasterBootRecord mbr = MasterBootRecord.ReadFromDisk(((MBRPartition)volume).Disk);
  52. return GetWindowsVolumeGuid(mbr.DiskSignature, (ulong)(partition.FirstSector * partition.BytesPerSector));
  53. }
  54. else if (volume is GPTPartition)
  55. {
  56. return ((GPTPartition)volume).VolumeGuid;
  57. }
  58. else if (volume is DynamicVolume)
  59. {
  60. return ((DynamicVolume)volume).VolumeGuid;
  61. }
  62. else if (volume is OperatingSystemVolume)
  63. {
  64. return ((OperatingSystemVolume)volume).VolumeGuid;
  65. }
  66. else
  67. {
  68. return null;
  69. }
  70. }
  71. public static Volume GetVolumeByGuid(Guid windowsVolumeGuid)
  72. {
  73. List<Volume> volumes = GetVolumes();
  74. foreach (Volume volume in volumes)
  75. {
  76. Guid? guid = GetWindowsVolumeGuid(volume);
  77. if (guid.HasValue && guid.Value == windowsVolumeGuid)
  78. {
  79. return volume;
  80. }
  81. }
  82. return null;
  83. }
  84. public static List<Volume> GetVolumes()
  85. {
  86. List<PhysicalDisk> disks = PhysicalDiskHelper.GetPhysicalDisks();
  87. List<Volume> result = new List<Volume>();
  88. // Get partitions:
  89. foreach (Disk disk in disks)
  90. {
  91. if (!DynamicDisk.IsDynamicDisk(disk))
  92. {
  93. List<Partition> partitions = BasicDiskHelper.GetPartitions(disk);
  94. foreach (Partition partition in partitions)
  95. {
  96. result.Add(partition);
  97. }
  98. }
  99. }
  100. // Get dynamic volumes
  101. List<DynamicVolume> dynamicVolumes = WindowsDynamicVolumeHelper.GetDynamicVolumes();
  102. foreach (DynamicVolume volume in dynamicVolumes)
  103. {
  104. result.Add(volume);
  105. }
  106. return result;
  107. }
  108. }
  109. }