Как получить информацию о геометрии жёсткого диска?

code: #csharp
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
 
namespace ConsoleApplication24
{
    class Program
    {
        const uint IOCTL_DISK_GET_DRIVE_GEOMETRY = 0x70000;
 
        const uint GENERIC_READ     = 0x80000000;
 
        const uint FILE_SHARE_READ  = 0x00000001;
        const uint FILE_SHARE_WRITE = 0x00000002;
 
        const uint OPEN_EXISTING    = 0x00000003;
 
        static readonly IntPtr INVALID_FILE_HANDLE = (IntPtr)( -1 );
 
        [DllImport( "kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode )]
        static extern IntPtr CreateFile (
            string lpFileName,
            uint dwDesiredAccess,
            uint dwShareMode,
            IntPtr lpSecurityAttributes,
            uint dwCreationDisposition,
            uint dwFlagsAndAttributes,
            IntPtr hTemplateFile
            );
 
        [DllImport( "kernel32.dll", SetLastError = true )]
        static extern bool CloseHandle (
            IntPtr hObject
            );
 
        enum MEDIA_TYPE : uint
        {
            Unknown = 0x00,
            F5_1Pt2_512 = 0x01,
            F3_1Pt44_512 = 0x02,
            F3_2Pt88_512 = 0x03,
            F3_20Pt8_512 = 0x04,
            F3_720_512 = 0x05,
            F5_360_512 = 0x06,
            F5_320_512 = 0x07,
            F5_320_1024 = 0x08,
            F5_180_512 = 0x09,
            F5_160_512 = 0x0a,
            RemovableMedia = 0x0b,
            FixedMedia = 0x0c,
            F3_120M_512 = 0x0d,
            F3_640_512 = 0x0e,
            F5_640_512 = 0x0f,
            F5_720_512 = 0x10,
            F3_1Pt2_512 = 0x11,
            F3_1Pt23_1024 = 0x12,
            F5_1Pt23_1024 = 0x13,
            F3_128Mb_512 = 0x14,
            F3_230Mb_512 = 0x15,
            F8_256_128 = 0x16,
            F3_200Mb_512 = 0x17,
            F3_240M_512 = 0x18,
            F3_32M_512 = 0x19
        }
 
        [StructLayout( LayoutKind.Sequential )]
        class DISK_GEOMETRY
        {
            public long Cylinders;
            public MEDIA_TYPE MediaType;
            public uint TracksPerCylinder;
            public uint SectorsPerTrack;
            public uint BytesPerSector;
        }
 
        [DllImport( "kernel32.dll", SetLastError = true )]
        static extern bool DeviceIoControl (
            IntPtr hDevice,
            uint dwIoControlCode,
            IntPtr lpInBuffer,
            int nInBufferSize,
            DISK_GEOMETRY lpOutBuffer,
            int nOutBufferSize,
            ref int lpBytesReturned,
            IntPtr lpOverlapped
            );
 
        static void Main ( string[] args ) {
            var geometry = new DISK_GEOMETRY();
            int retVal = Marshal.SizeOf( typeof( DISK_GEOMETRY ) );
 
            IntPtr hFile = CreateFile(
                "\\\\.\\PhysicalDrive0",
                GENERIC_READ,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                IntPtr.Zero,
                OPEN_EXISTING,
                0U,
                IntPtr.Zero
                );
 
            if ( hFile == INVALID_FILE_HANDLE )
                return;
 
            if ( !DeviceIoControl(
                    hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, IntPtr.Zero,
                    0, geometry, retVal, ref retVal, IntPtr.Zero 
                    )
                )
                Console.WriteLine( "Error: " + Marshal.GetLastWin32Error() );
 
            Debugger.Break();
 
            CloseHandle( hFile );
            Console.ReadLine();
        }
    }
}
Поделиться:

Похожие статьи: