次のコードを使用して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を実行しています。
参考文献
- MSDN:CreateFile関数
- MSDN:ファイル、パス、および名前空間の命名