gpt4 book ai didi

c# - Kernel32.dll 中的 CreateFile 不允许我打开物理磁盘

转载 作者:可可西里 更新时间:2023-11-01 13:28:52 24 4
gpt4 key购买 nike

我正在尝试使用以下代码从 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()文档:

The following requirements must be met for such a call to succeed:

  • The caller must have administrative privileges. For more information, see Running with Special Privileges.
  • The dwCreationDisposition parameter must have the OPEN_EXISTING flag.
  • When opening a volume or floppy disk, the dwShareMode parameter must have the FILE_SHARE_WRITE flag.

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

关于c# - Kernel32.dll 中的 CreateFile 不允许我打开物理磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4288031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com