private void CloseVolumes() { while (_volumeHandles.Count > 0) { var volumeHandle = _volumeHandles.Pop(); DiskUtils.UnlockVolume(volumeHandle); volumeHandle.Dispose(); } }
public DiskReader(DiskInfo diskInfo) { _diskInfo = diskInfo; _volumeHandles = new Stack <SafeFileHandle>(_diskInfo.Drives?.Length ?? 0); OpenVolumes(); _deviceHandle = DiskUtils.GetDeviceHandle(diskInfo.DiskNumber); _stream = new FileStream(_deviceHandle, FileAccess.Read, CommonSizes._128K, false); _disposed = false; _closed = false; _totalRead = 0; }
public static DiskInfo[] GetDisks(DriveInfo[] drives, bool onlyRemovable = true, bool onlyReady = true) { var diskInformation = new Dictionary <ushort, DiskInfo>(); foreach (var drive in drives) { if (onlyReady && !drive.IsReady) { continue; } if (onlyRemovable && drive.DriveType != DriveType.Removable) { continue; } var diskNumbers = DiskUtils.GetPhysicalDiskNumbersFromDrive(drive.RootDirectory.Name); foreach (var diskNumber in diskNumbers) { DiskInfo diskInfo; if (!diskInformation.TryGetValue(diskNumber, out diskInfo)) { diskInfo = new DiskInfo() { DiskNumber = diskNumber, Drives = new DriveInfo[0] }; diskInformation.Add(diskNumber, diskInfo); } diskInfo.Drives = Utils.Add <DriveInfo>(diskInfo.Drives, drive) .OrderBy(_ => _.Name) .ToArray(); } } foreach (var kvp in diskInformation) { kvp.Value.Size = DiskUtils.GetPhysicalDiskSize(kvp.Key); kvp.Value.SectorSize = DiskUtils.GetPhysicalDiskSectorSize(kvp.Key); kvp.Value.SectorCount = DiskUtils.GetPhysicalDiskSectorCount(kvp.Key); } var results = diskInformation.Values.OrderBy(_ => _.DiskNumber) .ToArray(); return(results); }
private void OpenVolumes() { if (!Utils.IsNullOrEmpty(_diskInfo.Drives)) { foreach (var drive in _diskInfo.Drives) { try { var volumeHandle = DiskUtils.GetVolumeHandle(drive.Name); if (volumeHandle == null) { throw new Exception($"Could not open volume: {drive.Name}"); } if (!DiskUtils.LockVolume(volumeHandle)) { volumeHandle.Close(); throw new Exception($"Could not lock volume: {drive.Name}"); } if (!DiskUtils.UnmountVolume(volumeHandle)) { DiskUtils.UnlockVolume(volumeHandle); volumeHandle.Close(); throw new Exception($"Could not unmount volume: {drive.Name}"); } _volumeHandles.Push(volumeHandle); } catch { CloseVolumes(); throw; } } } }