-
Notifications
You must be signed in to change notification settings - Fork 17
/
Volume.cs
216 lines (197 loc) · 7.51 KB
/
Volume.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// UsbEject version 1.0 March 2006
// written by Simon Mourier <email: simon [underscore] mourier [at] hotmail [dot] com>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace UsbEject.Library
{
/// <summary>
/// A volume device.
/// </summary>
public class Volume : Device, IComparable
{
private string _volumeName;
private string _logicalDrive;
private int[] _diskNumbers;
private List<Device> _disks;
private List<Device> _removableDevices;
internal Volume(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index)
:base(deviceClass, deviceInfoData, path, index)
{
}
/// <summary>
/// Gets the volume's name.
/// </summary>
public string VolumeName
{
get
{
if (_volumeName == null)
{
StringBuilder sb = new StringBuilder(1024);
if (!Native.GetVolumeNameForVolumeMountPoint(Path + "\\", sb, sb.Capacity))
{
// throw new Win32Exception(Marshal.GetLastWin32Error());
}
if (sb.Length > 0)
{
_volumeName = sb.ToString();
}
}
return _volumeName;
}
}
/// <summary>
/// Gets the volume's logical drive in the form [letter]:\
/// </summary>
public string LogicalDrive
{
get
{
if ((_logicalDrive == null) && (VolumeName != null))
{
((VolumeDeviceClass)DeviceClass)._logicalDrives.TryGetValue(VolumeName, out _logicalDrive);
}
return _logicalDrive;
}
}
/// <summary>
/// Gets a value indicating whether this volume is a based on USB devices.
/// </summary>
public override bool IsUsb
{
get
{
if (Disks != null)
{
foreach (Device disk in Disks)
{
if (disk.IsUsb)
return true;
}
}
return false;
}
}
/// <summary>
/// Gets a list of underlying disks for this volume.
/// </summary>
public List<Device> Disks
{
get
{
if (_disks == null)
{
_disks = new List<Device>();
if (DiskNumbers != null)
{
DiskDeviceClass disks = new DiskDeviceClass();
foreach (int index in DiskNumbers)
{
foreach (Device disk in disks.Devices)
{
if (disk.DiskNumber == index)
{
_disks.Add(disk);
}
}
}
}
}
return _disks;
}
}
public int[] DiskNumbers
{
get
{
if (_diskNumbers == null)
{
List<int> numbers = new List<int>();
if (LogicalDrive != null)
{
Console.WriteLine("Finding disk extents for volume: " + LogicalDrive);
IntPtr hFile = Native.CreateFile(@"\\.\" + LogicalDrive, 0, Native.FILE_SHARE_READ | Native.FILE_SHARE_WRITE, IntPtr.Zero, Native.OPEN_EXISTING, 0, IntPtr.Zero);
if (hFile.ToInt32() == Native.INVALID_HANDLE_VALUE)
throw new Win32Exception(Marshal.GetLastWin32Error());
int size = 0x400; // some big size
IntPtr buffer = Marshal.AllocHGlobal(size);
int bytesReturned = 0;
try
{
if (!Native.DeviceIoControl(hFile, Native.IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, IntPtr.Zero, 0, buffer, size, out bytesReturned, IntPtr.Zero))
{
// do nothing here on purpose
}
}
finally
{
Native.CloseHandle(hFile);
}
if (bytesReturned > 0)
{
int numberOfDiskExtents = (int)Marshal.PtrToStructure(buffer, typeof(int));
for (int i = 0; i < numberOfDiskExtents; i++)
{
IntPtr extentPtr = new IntPtr(buffer.ToInt32() + Marshal.SizeOf(typeof(long)) + i * Marshal.SizeOf(typeof(Native.DISK_EXTENT)));
Native.DISK_EXTENT extent = (Native.DISK_EXTENT)Marshal.PtrToStructure(extentPtr, typeof(Native.DISK_EXTENT));
numbers.Add(extent.DiskNumber);
}
}
Marshal.FreeHGlobal(buffer);
}
_diskNumbers = new int[numbers.Count];
numbers.CopyTo(_diskNumbers);
}
return _diskNumbers;
}
}
/// <summary>
/// Gets a list of removable devices for this volume.
/// </summary>
public override List<Device> RemovableDevices
{
get
{
if (_removableDevices == null)
{
_removableDevices = new List<Device>();
if (Disks == null)
{
_removableDevices = base.RemovableDevices;
}
else
{
foreach (Device disk in Disks)
{
foreach (Device device in disk.RemovableDevices)
{
_removableDevices.Add(device);
}
}
}
}
return _removableDevices;
}
}
/// <summary>
/// Compares the current instance with another object of the same type.
/// </summary>
/// <param name="obj">An object to compare with this instance.</param>
/// <returns>A 32-bit signed integer that indicates the relative order of the comparands.</returns>
public override int CompareTo(object obj)
{
Volume device = obj as Volume;
if (device == null)
throw new ArgumentException();
if (LogicalDrive == null)
return 1;
if (device.LogicalDrive == null)
return -1;
return LogicalDrive.CompareTo(device.LogicalDrive);
}
}
}