日期:2014-05-17  浏览次数:20997 次

c#语言如何读取mbr的信息?
这里是我从网上找的代码。想问为什么用c#读取mbr返回的byte[]为空呢?参数是哪里错了呢?
 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 = 0x00000001;
        public const uint CREATE_ALWAYS = 0x00000002;
        public const uint OPEN_EXISTING = 3;
        public const uint FILE_BEGIN = 0;
        public const uint FILE_CURRENT = 1;
        public const uint FILE_END = 2;
public static byte[] ReadMBR()
        {
            //HANDLE hDev=CreateFile("\\\\.\\A:",GENERIC_READ,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
             
            IntPtr DiskHandle = CreateFile(@"\\.\PhysicalDrive0", GENERIC_READ,
                CREATE_NEW|CREATE_ALWAYS, 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;  
        }
c# mbr