You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何确定Windows操作系统的启动盘符?

编程获取Windows启动驱动器的解决方案

核心原理

Windows的启动驱动器就是系统安装所在的盘符,所有方案本质都是读取系统预定义的系统路径相关配置,提取盘符即可。

Delphi 实现方案

你可以直接选用以下任意一种方式实现,无需引入额外依赖:

  1. 读取系统环境变量(最简洁)
uses
  SysUtils;

function GetBootDrive: string;
begin
  Result := GetEnvironmentVariable('SystemDrive');
end;
  1. 调用系统API获取Windows目录提取盘符
uses
  Windows;

function GetBootDrive: string;
var
  winDir: array[0..MAX_PATH] of Char;
begin
  GetWindowsDirectory(@winDir[0], MAX_PATH);
  Result := Copy(winDir, 1, 2);
end;
  1. 读取注册表获取系统根路径提取盘符
uses
  Registry, SysUtils;

function GetBootDrive: string;
var
  reg: TRegistry;
begin
  reg := TRegistry.Create;
  try
    reg.RootKey := HKEY_LOCAL_MACHINE;
    if reg.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows NT\CurrentVersion') then
    begin
      Result := Copy(reg.ReadString('SystemRoot'), 1, 2);
      reg.CloseKey;
    end;
  finally
    reg.Free;
  end;
end;

其他编程语言通用方案

所有编程语言都可以通过以下通用思路实现,无需依赖特殊框架:

  • 直接读取系统内置环境变量SystemDrive,返回值就是启动盘符(如C:),实现成本最低
  • 调用Windows原生API GetWindowsDirectoryW / GetSystemDirectoryW,获取系统目录后提取前两位字符
  • 读取注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion下的SystemRoot值,提取盘符即可

不同语言示例

  1. C/C++
#include <windows.h>
#include <stdio.h>

int main() {
    char bootDrive[4] = {0};
    GetEnvironmentVariableA("SystemDrive", bootDrive, 3);
    printf("启动驱动器:%s\n", bootDrive);
    return 0;
}
  1. Python
import os
print(os.getenv("SystemDrive"))
  1. PowerShell
$env:SystemDrive
  1. C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Environment.GetEnvironmentVariable("SystemDrive"));
    }
}

内容的提问来源于stack exchange,提问作者Optimax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 14:42:04