如何判断 Windows 用于启动哪个磁盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:11:48  来源:igfitidea点击:

How to tell which disk Windows Used to Boot

windowsmbr

提问by Terry

I'm need to find a method to programmatically determine which disk drive Windows is using to boot. In other words, I need a way from Windows to determine which drive the BIOS is using to boot the whole system.

我需要找到一种方法来以编程方式确定 Windows 用于启动的磁盘驱动器。换句话说,我需要从 Windows 中确定 BIOS 使用哪个驱动器来引导整个系统的方法。

Does Windows expose an interface to discover this? With how big the Windows API is, I'm hoping there is something buried in there that might do the trick.

Windows 是否公开了一个接口来发现这一点?Windows API 有多大,我希望有一些东西可以解决这个问题。

Terry

特里

p.s. Just reading the first sectors of the hard disk isn't reveling anything. On my dev box I have two hard disks, and when I look at the contents of the first couple of sectors on either of the hard disks I have a standard boiler plate MBR.

ps 只是读取硬盘的第一个扇区并没有带来任何惊喜。在我的开发箱上,我有两个硬盘,当我查看其中一个硬盘上前几个扇区的内容时,我有一个标准的样板 MBR。

Edit to clarify a few things. The way I want to identify the device is with a string which will identify a physical disk drive (as opposed to a logical disk drive). Physical disk drives are of the form "\\.\PHYSICALDRIVEx" where x is a number. On the other hand, a logical drive is identified by a string of the form, "\\.\x" where x is a drive letter.

编辑以澄清一些事情。我想识别设备的方式是使用一个字符串来识别物理磁盘驱动器(而不是逻辑磁盘驱动器)。物理磁盘驱动器的格式为“\\.\PHYSICALDRIVEx”,其中 x 是一个数字。另一方面,逻辑驱动器由“\\.\x”形式的字符串标识,其中 x 是驱动器号。

Edit to discuss a few of the ideas that were thrown out. Knowing which logical volume Windows used to boot doesn't help me here. Here is the reason. Assume that C: is using a mirrored RAID setup. Now, that means we have at least two physical drives. Now, I get the mapping from Logical Drive to Physical Drive and I discover that there are two physical drives used by that volume. Which one did Windows use to boot? Of course, this is assuming that the physical drive Windows used to boot is the same physical drive that contains the MBR.

编辑讨论一些被抛弃的想法。知道 Windows 用来引导哪个逻辑卷在这里对我没有帮助。这是原因。假设 C: 正在使用镜像 RAID 设置。现在,这意味着我们至少有两个物理驱动器。现在,我获得了从逻辑驱动器到物理驱动器的映射,我发现该卷使用了两个物理驱动器。Windows 使用哪一个启动?当然,这是假设用于引导 Windows 的物理驱动器与包含 MBR 的物理驱动器相同。

回答by JD.

  1. Go into Control Panel
  2. System and Security
  3. Administrative Tools
  4. Launch the System Configurationtool
  1. 进入 Control Panel
  2. System and Security
  3. Administrative Tools
  4. 启动System Configuration工具

If you have multiple copies of Windows installed, the one you are booted with will be named such as:

如果您安装了多个 Windows 副本,则启动时使用的副本将命名为:

Windows 7 (F:\Windows)
Windows 7 (C:\Windows) : Current OS, Default OS

回答by UnkwnTech

Unless C: is not the drive that windows booted from.
Parse the %SystemRoot% variable, it contains the location of the windows folder (i.e. c:\windows).

除非 C: 不是 Windows 启动的驱动器。
解析 %SystemRoot% 变量,它包含 windows 文件夹的位置(即 c:\windows)。

回答by Chris Gillum

You can use WMI to figure this out. The Win32_BootConfigurationclass will tell you both the logical drive and the physical device from which Windows boots. Specifically, the Caption property will tell you which deviceyou're booting from.

您可以使用 WMI 来解决这个问题。该Win32_BootConfiguration类会告诉你这两个逻辑驱动器和物理设备从Windows启动。具体来说,Caption 属性会告诉您从哪个设备启动。

For example, in powershell, just type gwmi Win32_BootConfigurationto get your answer.

例如,在 powershell 中,只需键入gwmi Win32_BootConfiguration即可获得答案。

回答by Mark Brackett

That depends on your definition of which disk drive Windows used to boot. I can think of 3 different answers on a standard BIOS system (who knows what an EFI system does):

这取决于您对 Windows 用于启动哪个磁盘驱动器的定义。我可以在标准 BIOS 系统上想到 3 个不同的答案(谁知道 EFI 系统是做什么的):

  1. The drive that contains the active MBR
  2. The active partition, with NTLDR (the system partition)
  3. The partition with Windows on it (the boot partition)
  1. 包含活动 MBR 的驱动器
  2. 活动分区,带NTLDR(系统分区)
  3. 带有 Windows 的分区(引导分区)

2 and 3 should be easy to find - I'm not so sure about 1. Though you can raw disk read to find an MBR, that doesn't mean it's the BIOS boot device this time or even next time (you could have multiple disks with MBRs).

2 和 3 应该很容易找到 - 我对 1 不太确定。虽然您可以读取原始磁盘以找到 MBR,但这并不意味着这次或什至下一次它是 BIOS 引导设备(您可以有多个带有 MBR 的磁盘)。

You really can't even be sure that the PC was started from a hard drive - it's perfectly possible to boot Windows from a floppy. In that case, both 1 and 2 would technically be a floppy disk, though 3 would remain C:\Windows.

您甚至无法确定 PC 是从硬盘驱动器启动的 - 从软盘启动 Windows 是完全可能的。在这种情况下,1 和 2 从技术上讲都是软盘,但 3 将保留在 C:\Windows 中。

You might need to be a bit more specific in your requirements or goals.

您可能需要更具体地说明您的要求或目标。

回答by Rob Walker

There is no boot.ini on a machine with just Vista installed.

只安装了 Vista 的机器上没有 boot.ini。

How do you want to identify the drive/partition: by the windows drive letter it is mapped to (eg. c:\, d:) or by how its hardware signature (which bus, etc).

您想如何识别驱动器/分区:通过它映射到的 Windows 驱动器号(例如 c:\、d:)或通过它的硬件签名方式(哪个总线等)。

For the simple case check out GetSystemDirectory

对于简单的情况,请查看GetSystemDirectory

回答by Duncan Smart

Try HKEY_LOCAL_MACHINE\SYSTEM\Setup\SystemPartition

试试 HKEY_LOCAL_MACHINE\SYSTEM\Setup\SystemPartition

回答by ST3

You can try use simple command line. bcdeditis what you need, just run cmd as administrator and type bcdeditor bcdedit \v, this doesn't work on XP, but hope it is not an issue.

您可以尝试使用简单的命令行。bcdedit就是你所需要的,只需以管理员身份运行 cmd 并输入bcdeditor bcdedit \v,这在 XP 上不起作用,但希望它不是问题。

Anyway for XP you can take a look into boot.inifile.

无论如何,对于 XP,您可以查看boot.ini文件。