5

次のコードを使用してMBRを取得しようとしていますPhysicalDrive0

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 Server2008R2を実行しています。

参考文献

4

1 に答える 1

10

CreateFile()ドキュメントから:

このような呼び出しを成功させるには、次の要件を満たす必要があります。

  • 発信者には管理者権限が必要です。詳細については、「特別な権限で実行する」を参照してください。
  • dwCreationDispositionパラメータには フラグOPEN_EXISTING必要です。
  • ボリュームまたはフロッピーディスクを開くときは、dwShareModeパラメータにFILE_SHARE_WRITEフラグが必要です。

を渡してみてECreationDisposition.OpenExistingくださいdwCreationDisposition

于 2010-11-26T21:16:57.373 に答える