コード例 #1
0
ファイル: DiskReader.cs プロジェクト: Workshell/disk-imager
        private void CloseVolumes()
        {
            while (_volumeHandles.Count > 0)
            {
                var volumeHandle = _volumeHandles.Pop();

                DiskUtils.UnlockVolume(volumeHandle);
                volumeHandle.Dispose();
            }
        }
コード例 #2
0
ファイル: DiskReader.cs プロジェクト: Workshell/disk-imager
        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;
        }
コード例 #3
0
ファイル: DiskInfo.cs プロジェクト: Workshell/disk-imager
        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);
        }
コード例 #4
0
ファイル: DiskReader.cs プロジェクト: Workshell/disk-imager
        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;
                    }
                }
            }
        }