Beispiel #1
0
        public static char GetFirstDriveLetterFromMask(uint driveMask, bool checkDriveType = true)
        {
            for (int i = 0; driveMask != 0; i++)
            {
                if ((driveMask & 0x01) != 0)
                {
                    try
                    {
                        if (checkDriveType)
                        {
                            if (NativeDiskWrapper.CheckDriveType(string.Format(@"\\.\{0}:\", (char)('A' + i))) != 0)
                            {
                                return((char)('A' + i));
                            }
                        }
                        else
                        {
                            return((char)('A' + i));
                        }
                    }
                    catch { }
                }
                driveMask >>= 1;
            }

            return((char)0);
        }
Beispiel #2
0
        public Disk(char[] driveLetters)
        {
            deviceIDs     = new int[driveLetters.Length];
            volumeIDs     = new int[driveLetters.Length];
            volumeHandles = new IntPtr[driveLetters.Length];
            deviceHandles = new IntPtr[driveLetters.Length];

            DriveLetters = driveLetters;

            for (int i = 0; i < driveLetters.Length; i++)
            {
                deviceIDs[i]     = NativeDiskWrapper.CheckDriveType(string.Format(@"\\.\{0}:\", DriveLetters[i]));
                volumeIDs[i]     = DriveLetters[i] - 'A';
                volumeHandles[i] = deviceHandles[i] = IntPtr.Zero;
            }

            Utils.PreventComputerSleep();
        }
Beispiel #3
0
        public static char[] GetLogicalDrives()
        {
            List <char> drives     = new List <char>();
            uint        drivesMask = NativeDiskWrapper.GetLogicalDrives();

            for (int i = 0; drivesMask != 0; i++)
            {
                if ((drivesMask & 0x01) != 0)
                {
                    try
                    {
                        if (NativeDiskWrapper.CheckDriveType(string.Format(@"\\.\{0}:\", (char)('A' + i))) != 0)
                        {
                            drives.Add((char)('A' + i));
                        }
                    }
                    catch { }
                }
                drivesMask >>= 1;
            }

            return(drives.ToArray());
        }
Beispiel #4
0
        public static DiskPartitionInfo GetDiskPartitionInfo(char driveLetter)
        {
            int    deviceID           = NativeDiskWrapper.CheckDriveType(string.Format(@"\\.\{0}:\", driveLetter));
            IntPtr deviceHandle       = NativeDiskWrapper.GetHandleOnDevice(deviceID, NativeDisk.GENERIC_READ);
            uint   partitionTableSize = 0;

            DiskPartitionInfo partitionsInfo = new DiskPartitionInfo();

            var partitionInfo = NativeDiskWrapper.GetDiskPartitionInfo(deviceHandle);

            partitionsInfo.DiskTotalSize = GetDeviceLength(deviceID);

            if (partitionInfo.PartitionStyle == PARTITION_STYLE.MasterBootRecord)
            {
                partitionsInfo.PartitionType = PartitionType.MBR;
                partitionTableSize           = 512;

                unsafe
                {
                    byte *data = NativeDiskWrapper.ReadSectorDataPointerFromHandle(deviceHandle, 0, 1, 512);

                    for (int i = 0; i < 4; i++)
                    {
                        ulong partitionStartSector = (uint)Marshal.ReadInt32(new IntPtr(data), 0x1BE + 8 + 16 * i);
                        ulong partitionNumSectors  = (uint)Marshal.ReadInt32(new IntPtr(data), 0x1BE + 12 + 16 * i);

                        if (partitionStartSector + partitionNumSectors > 0)
                        {
                            partitionsInfo.PartitionSizes.Add(partitionNumSectors * 512);
                        }
                    }
                }
            }
            else if (partitionInfo.PartitionStyle == PARTITION_STYLE.GuidPartitionTable)
            {
                partitionsInfo.PartitionType = PartitionType.GPT;
                partitionTableSize           = 17408;

                unsafe
                {
                    byte *data            = NativeDiskWrapper.ReadSectorDataPointerFromHandle(deviceHandle, 0, 1, 512);
                    uint  gptHeaderOffset = (uint)Marshal.ReadInt32(new IntPtr(data), 0x1C6);
                    data = NativeDiskWrapper.ReadSectorDataPointerFromHandle(deviceHandle, gptHeaderOffset, 1, 512);
                    ulong partitionTableSector = (ulong)Marshal.ReadInt64(new IntPtr(data), 0x48);
                    uint  noOfPartitionEntries = (uint)Marshal.ReadInt32(new IntPtr(data), 0x50);
                    uint  sizeOfPartitionEntry = (uint)Marshal.ReadInt32(new IntPtr(data), 0x54);

                    data = NativeDiskWrapper.ReadSectorDataPointerFromHandle(deviceHandle, partitionTableSector, (512 / sizeOfPartitionEntry) * noOfPartitionEntries, 512);

                    for (int i = 0; i < noOfPartitionEntries; i++)
                    {
                        ulong partitionStartSector = (ulong)Marshal.ReadInt64(new IntPtr(data), (int)(0x20 + sizeOfPartitionEntry * i));
                        ulong partitionNumSectors  = (ulong)Marshal.ReadInt64(new IntPtr(data), (int)(0x28 + sizeOfPartitionEntry * i));

                        if (partitionStartSector + partitionNumSectors > 0)
                        {
                            partitionsInfo.PartitionSizes.Add(partitionNumSectors * 512);
                        }
                    }
                }
            }
            else
            {
                partitionsInfo.PartitionType = PartitionType.RAW;
            }

            NativeDisk.CloseHandle(deviceHandle);

            partitionsInfo.UnallocatedSize = partitionsInfo.DiskTotalSize - (partitionsInfo.PartitionSizes.Sum() + partitionTableSize);

            return(partitionsInfo);
        }