我正在尝试使用以下代码从PhysicalDrive0获取 MBR :

private static byte[] ReadMbr(string lpFileName)
{
   byte[] mbr = new byte[512];

   using (SafeFileHandle drive = CreateFile(
         lpFileName: lpFileName,
         dwDesiredAccess: (uint) EFileAccess.GenericRead, //DO NOT MODIFY THE MBR!!!
         dwShareMode: (uint)EFileShare.Write | (uint)EFileShare.Read | (uint)EFileShare.Delete,
         SecurityAttributes: IntPtr.Zero,
         dwCreationDisposition: (uint) ECreationDisposition.OpenAlways,
         dwFlagsAndAttributes: (uint)EFileAttributes.System,
         hTemplateFile: IntPtr.Zero))
   {
      if (drive.IsInvalid)
         throw new IOException("Unable to access drive. Win32 Error Code " + Marshal.GetLastWin32Error());

      //Get the 1st 512 bytes of the volume (MBR)
      using (FileStream stream = new FileStream(drive, FileAccess.Read))
      {
         stream.Read(mbr, 0, 512);
      }
   }

   return mbr;
}

我尝试过
  • \\.\PhysicalDisk0
  • \\.\PhysicalDrive0
  • \\.\PhysicalDisk0:
  • \\.\PhysicalDrive0

  • 他们都不起作用。我以管理员身份运行。我还可以使\\.\C:正常工作并显示VBR,而不会出现任何问题。

    作为记录:

    -我正在运行Windows Server 2008 R2。

    引用
  • MSDN:CreateFile function
  • MSDN:Naming Files, Paths, and Namespaces
  • 最佳答案

    CreateFile()文档中:



    您可能想要尝试在ECreationDisposition.OpenExisting中传递dwCreationDisposition

    08-26 19:02