转,原文地址https://blog.csdn.net/hellokandy/article/details/52242365
常用的系统环境变量有:
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 来获取环境变量:
1 #include <Windows.h> 2 #include <iostream> 3 4 int main() 5 { 6 wchar_t buffer[MAX_PATH]; 7 DWORD code = GetEnvironmentVariable(L"WINDIR", buffer, MAX_PATH); 8 std::wcout << L"WINDIR:\t\t\t" << buffer << std::endl; 9 10 GetEnvironmentVariable(L"SYSTEMROOT", buffer, MAX_PATH); 11 std::wcout << L"SYSTEMROOT:\t\t" << buffer << std::endl; 12 13 GetEnvironmentVariable(L"SYSTEMDRIVE", buffer, MAX_PATH); 14 std::wcout << L"SYSTEMDRIVE:\t\t" << buffer << std::endl; 15 16 GetEnvironmentVariable(L"HOMEDRIVE", buffer, MAX_PATH); 17 std::wcout << L"HOMEDRIVE:\t\t" << buffer << std::endl; 18 19 GetEnvironmentVariable(L"USERPROFILE", buffer, MAX_PATH); 20 std::wcout << L"USERPROFILE:\t\t" << buffer << std::endl; 21 22 GetEnvironmentVariable(L"HOMEPATH", buffer, MAX_PATH); 23 std::wcout << L"HOMEPATH:\t\t" << buffer << std::endl; 24 25 GetEnvironmentVariable(L"TMP", buffer, MAX_PATH); 26 std::wcout << L"TMP:\t\t\t" << buffer << std::endl; 27 28 GetEnvironmentVariable(L"TEMP", buffer, MAX_PATH); 29 std::wcout << L"TEMP:\t\t\t" << buffer << std::endl; 30 31 GetEnvironmentVariable(L"APPDATA", buffer, MAX_PATH); 32 std::wcout << L"APPDATA:\t\t" << buffer << std::endl; 33 34 GetEnvironmentVariable(L"PROGRAMFILES", buffer, MAX_PATH); 35 std::wcout << L"PROGRAMFILES:\t\t" << buffer << std::endl; 36 37 GetEnvironmentVariable(L"COMMONPROGRAMFILES", buffer, MAX_PATH); 38 std::wcout << L"COMMONPROGRAMFILES:\t" << buffer << std::endl; 39 40 GetEnvironmentVariable(L"USERNAME", buffer, MAX_PATH); 41 std::wcout << L"USERNAME:\t\t" << buffer << std::endl; 42 43 GetEnvironmentVariable(L"ALLUSERSPROFILE", buffer, MAX_PATH); 44 std::wcout << L"ALLUSERSPROFILE:\t" << buffer << std::endl; 45 46 GetEnvironmentVariable(L"OS", buffer, MAX_PATH); 47 std::wcout << L"OS:\t\t\t" << buffer << std::endl; 48 49 GetEnvironmentVariable(L"COMPUTERNAME", buffer, MAX_PATH); 50 std::wcout << L"COMPUTERNAME:\t\t" << buffer << std::endl; 51 52 GetEnvironmentVariable(L"NUMBER_OF_PROCESSORS", buffer, MAX_PATH); 53 std::wcout << L"NUMBER_OF_PROCESSORS:\t" << buffer << std::endl; 54 55 GetEnvironmentVariable(L"PROCESSOR_ARCHITECTURE", buffer, MAX_PATH); 56 std::wcout << L"PROCESSOR_ARCHITECTURE:\t" << buffer << std::endl; 57 58 GetEnvironmentVariable(L"PROCESSOR_LEVEL", buffer, MAX_PATH); 59 std::wcout << L"PROCESSOR_LEVEL:\t" << buffer << std::endl; 60 61 GetEnvironmentVariable(L"PROCESSOR_REVISION", buffer, MAX_PATH); 62 std::wcout << L"PROCESSOR_REVISION:\t" << buffer << std::endl; 63 64 GetEnvironmentVariable(L"USERDOMAIN", buffer, MAX_PATH); 65 std::wcout << L"USERDOMAIN:\t\t" << buffer << std::endl; 66 67 GetEnvironmentVariable(L"COMSPEC", buffer, MAX_PATH); 68 std::wcout << L"COMSPEC:\t\t" << buffer << std::endl; 69 70 GetEnvironmentVariable(L"PATHEXT", buffer, MAX_PATH); 71 std::wcout << L"PATHEXT:\t\t" << buffer << std::endl; 72 73 GetEnvironmentVariable(L"PATH", buffer, MAX_PATH); 74 std::wcout << L"PATH:\t\t" << buffer << std::endl; 75 76 system("pause"); 77 return 0; 78 }