【发布时间】:2010-11-26 19:24:10
【问题描述】:
我正在尝试使用以下代码从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。
参考
【问题讨论】:
-
您收到的 Win32 错误代码是什么?
-
根据我使用的输入,它给了我 2 和 87。
标签: c# .net windows pinvoke kernel32