Ejemplo n.º 1
0
        public static int GetBytesPerSector(SafeFileHandle hDevice)
        {
            long          size;
            DISK_GEOMETRY diskGeometry = GetDiskGeometryAndSize(hDevice, out size);

            return((int)diskGeometry.BytesPerSector);
        }
Ejemplo n.º 2
0
        public static DISK_GEOMETRY GetDiskGeometryAndSize(SafeFileHandle hDevice, out long diskSize)
        {
            DISK_GEOMETRY diskGeometry = GetDiskGeometry(hDevice);

            diskSize = GetPartitionSize(hDevice);
            return(diskGeometry);
        }
Ejemplo n.º 3
0
 public static DISK_GEOMETRY GetDiskGeometryAndSize(SafeFileHandle hDevice, out long diskSize)
 {
     if (Environment.OSVersion.Version >= new Version(5, 1, 0, 0))
     {
         // XP and upward
         DISK_GEOMETRY_EX diskGeometryEx = GetDiskGeometryEx(hDevice);
         diskSize = diskGeometryEx.DiskSize;
         return(diskGeometryEx.Geometry);
     }
     else
     {
         // Windows 2000
         DISK_GEOMETRY diskGeometry = GetDiskGeometry(hDevice);
         diskSize = GetPartitionSize(hDevice);
         return(diskGeometry);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Supported on Windows 2000
        /// </summary>
        public static DISK_GEOMETRY GetDiskGeometry(SafeFileHandle hDevice)
        {
            uint          dummy;
            DISK_GEOMETRY diskGeometry = new DISK_GEOMETRY();
            IntPtr        lpOutBuffer  = Marshal.AllocHGlobal(Marshal.SizeOf(diskGeometry));

            Marshal.StructureToPtr(diskGeometry, lpOutBuffer, false);
            bool success = DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY, IntPtr.Zero, 0,
                                           lpOutBuffer, (uint)Marshal.SizeOf(typeof(DISK_GEOMETRY)), out dummy, IntPtr.Zero);

            diskGeometry = (DISK_GEOMETRY)Marshal.PtrToStructure(lpOutBuffer, typeof(DISK_GEOMETRY));
            Marshal.FreeHGlobal(lpOutBuffer);
            if (!success)
            {
                throw new IOException("Unable to retrieve disk geometry");
            }
            return(diskGeometry);
        }
Ejemplo n.º 5
0
        private void PopulateDiskInfo()
        {
            bool           releaseHandle;
            SafeFileHandle handle = PhysicalDiskHandlePool.ObtainHandle(m_physicalDiskIndex, FileAccess.Read, ShareMode.ReadWrite, out releaseHandle);

            if (!handle.IsInvalid)
            {
                if (!PhysicalDiskUtils.IsMediaAccesible(handle))
                {
                    throw new DeviceNotReadyException();
                }
                DISK_GEOMETRY diskGeometry = PhysicalDiskUtils.GetDiskGeometryAndSize(handle, out m_size);
                if (releaseHandle)
                {
                    PhysicalDiskHandlePool.ReleaseHandle(m_physicalDiskIndex);
                }
                m_bytesPerSector = (int)diskGeometry.BytesPerSector;

                // CHS:
                m_cylinders         = diskGeometry.Cylinders;
                m_tracksPerCylinder = (int)diskGeometry.TracksPerCylinder;
                m_sectorsPerTrack   = (int)diskGeometry.SectorsPerTrack;
            }
            else
            {
                // we always release invalid handle
                PhysicalDiskHandlePool.ReleaseHandle(m_physicalDiskIndex);

                // get error code and throw
                int    errorCode = Marshal.GetLastWin32Error();
                string message   = String.Format("Can't read from disk {0}", m_physicalDiskIndex);
                if (errorCode == (int)Win32Error.ERROR_FILE_NOT_FOUND)
                {
                    throw new DriveNotFoundException(message);
                }
                else
                {
                    FileStreamEx.ThrowIOError(errorCode, message);
                }
            }
        }