roger 发表于 2023-7-3 15:47:38

根据系统环境变量获取真实路径

常用的系统环境变量:

WINDIR: //系统目录 - C:\WINDOWS
SYSTEMROOT: //系统目录 - C:\WINDOWS
SYSTEMDRIVE:    //系统根目录 - C:
HOMEDRIVE:    //当前用户根目录 - C:
USERPROFILE:    //当前用户目录 - C:\Users\Kandy
HOMEPATH:    //当前用户路径 - \Users\Kandy
TMP:    //当前用户临时文件夹 - C:\Users\Kandy\AppData\Local\Temp
TEMP:    //当前用户临时文件夹 - C:\Users\Kandy\AppData\Local\Temp
APPDATA:    //当前用户数据文件夹 - C:\Users\Kandy\AppData\Roaming
PROGRAMFILES:    //程序默认安装目录 - C:\Program Files (x86)
COMMONPROGRAMFILES:    //文件通用目录 - C:\Program Files (x86)\Common Files
USERNAME:    //当前用户名 - Kandy
ALLUSERSPROFILE:    //所有用户文件目录 - C:\ProgramData
OS: //操作系统名 - Windows_NT
COMPUTERNAME: //计算机名 - KANDY-PC
NUMBER_OF_PROCESSORS: //处理器个数 - 4
PROCESSOR_ARCHITECTURE: //处理器芯片架构 - x86
PROCESSOR_LEVEL:    //处理器型号 - 6
PROCESSOR_REVISION: //处理器修订号 - 3c03
USERDOMAIN: //包含用户帐号的域 - KANDY-PC
COMSPEC:    //C:\WINDOWS\system32\cmd.exe
PATHEXT:    //执行文件类型 - .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PATH:    //搜索路径使用 GetEnvironmentVariable 来获取环境变量:
#include <Windows.h>
#include <iostream>
int main()
{
    wchar_t buffer;
    DWORD code = GetEnvironmentVariable(L"WINDIR", buffer, MAX_PATH);
    GetEnvironmentVariable(L"SYSTEMROOT", buffer, MAX_PATH);
    GetEnvironmentVariable(L"SYSTEMDRIVE", buffer, MAX_PATH);
    GetEnvironmentVariable(L"HOMEDRIVE", buffer, MAX_PATH);
    GetEnvironmentVariable(L"USERPROFILE", buffer, MAX_PATH);
    GetEnvironmentVariable(L"HOMEPATH", buffer, MAX_PATH);
    GetEnvironmentVariable(L"TMP", buffer, MAX_PATH);
    GetEnvironmentVariable(L"TEMP", buffer, MAX_PATH);
    GetEnvironmentVariable(L"APPDATA", buffer, MAX_PATH);
    GetEnvironmentVariable(L"PROGRAMFILES", buffer, MAX_PATH);
    GetEnvironmentVariable(L"COMMONPROGRAMFILES", buffer, MAX_PATH);
    GetEnvironmentVariable(L"USERNAME", buffer, MAX_PATH);
    GetEnvironmentVariable(L"ALLUSERSPROFILE", buffer, MAX_PATH);
    GetEnvironmentVariable(L"OS", buffer, MAX_PATH);
    GetEnvironmentVariable(L"COMPUTERNAME", buffer, MAX_PATH);
    GetEnvironmentVariable(L"NUMBER_OF_PROCESSORS", buffer, MAX_PATH);
    GetEnvironmentVariable(L"PROCESSOR_ARCHITECTURE", buffer, MAX_PATH);
    GetEnvironmentVariable(L"PROCESSOR_LEVEL", buffer, MAX_PATH);
    GetEnvironmentVariable(L"PROCESSOR_REVISION", buffer, MAX_PATH);
    GetEnvironmentVariable(L"USERDOMAIN", buffer, MAX_PATH);
    GetEnvironmentVariable(L"COMSPEC", buffer, MAX_PATH);
    GetEnvironmentVariable(L"PATHEXT", buffer, MAX_PATH);
    GetEnvironmentVariable(L"PATH", buffer, MAX_PATH);
    return 0;
}

页: [1]
查看完整版本: 根据系统环境变量获取真实路径