C#如何读取磁盘扇区?困扰了我好几天的问题了帮帮小弟吧最好能帖上源代码.

解决方案 »

  1.   

    介绍你一个地方,去“叶帆工作室”看看
    http://blog.csdn.net/yefanqiu/archive/2008/03/19/2198364.aspx
      

  2.   

    用ReadFile就可以了,下面是我写的读MBR记录的C#代码
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;namespace DiskInfo
    {
        public static class Native
        {
            public const short FILE_ATTRIBUTE_NORMAL = 0x80;
            public const short INVALID_HANDLE_VALUE = -1;
            public const uint GENERIC_READ = 0x80000000;
            public const uint GENERIC_WRITE = 0x40000000;
            public const uint CREATE_NEW = 1;
            public const uint CREATE_ALWAYS = 2;
            public const uint OPEN_EXISTING = 3;
            public const uint FILE_BEGIN = 0;
            public const uint FILE_CURRENT=1;
            public const uint FILE_END = 2;        [DllImport("Kernel32.dll")]
            extern static IntPtr CreateFile(string fileName, uint accessFlag, uint shareMode, IntPtr security,
                uint createFlag, uint attributeFlag, IntPtr tempfile);        [DllImport("Kernel32.dll")]
            extern static bool ReadFile(IntPtr handle, [Out] byte[] buffer, uint bufferLength, 
                ref uint length, IntPtr overLapped);        [DllImport("Kernel32.dll")]
            extern static bool CloseHandle(IntPtr handle);        [DllImport("Kernel32.dll")]
            extern static uint SetFilePointer(IntPtr handle, int offset, IntPtr distance, uint flag);
            
            /// <summary>
            /// Read Disk MBR(Main Boot Record)
            /// </summary>
            /// <returns>512 byte for MBR record</returns>
            public static byte[] ReadMBR()
            {
                //HANDLE hDev=CreateFile("\\\\.\\A:",GENERIC_READ,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);            IntPtr DiskHandle = CreateFile(@"\\.\PhysicalDrive0", GENERIC_READ,
                    0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
                byte[] buffer = new byte[512];
                uint length =0;
                SetFilePointer(DiskHandle, 0, IntPtr.Zero, FILE_BEGIN);
                ReadFile(DiskHandle, buffer, 512, ref length, IntPtr.Zero);
                CloseHandle(DiskHandle);
                return buffer;
            }
        }
    }