我正在尝试使用以下代码从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。
引用
最佳答案
从CreateFile()文档中:
您可能想要尝试在ECreationDisposition.OpenExisting
中传递dwCreationDisposition
。