.net 可以通过 Environment 对象获取到 机器环境变量,用户环境变量,进程环境变量 等信息
机器环境变量存储与注册表 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment 中
用户环境变量存储与注册表 HKEY_CURRENT_USER\Environment
#region 环境变量
/// <summary>
/// 环境变量信息
/// </summary>
/// <returns></returns>
public string SysEnvironment()
{
StringBuilder str = new StringBuilder();
//环境变量
// HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
IDictionary dicMachine = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine);
str.AppendLine(string.Format("{0}: ", "机器环境变量"));
foreach (string key in dicMachine.Keys)
{
string val = dicMachine[key].ToString();
str.AppendLine(string.Format("{0}: {1}", key, val));
}
str.AppendLine(string.Format("{0}", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"));
// 环境变量存储在 Windows 操作系统注册表的 HKEY_CURRENT_USER\Environment 项中,或从其中检索。
IDictionary dicUser = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User);
str.AppendLine(string.Format("{0}: ", "用户环境变量"));
foreach (string key in dicUser.Keys)
{
string val = dicUser[key].ToString();
str.AppendLine(string.Format("{0}: {1}", key, val));
}
str.AppendLine(string.Format("{0}", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"));
IDictionary dicProcess = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process);
str.AppendLine(string.Format("{0}: ", "进程环境变量"));
foreach (string key in dicProcess.Keys)
{
string val = dicProcess[key].ToString();
str.AppendLine(string.Format("{0}: {1}", key, val));
}
return str.ToString();
}
#endregion
执行后输出结果:
机器环境变量:
ComSpec: C:\WINDOWS\system32\cmd.exe
TEMP: C:\WINDOWS\TEMP
PROCESSOR_ARCHITECTURE: AMD64
OS: Windows_NT
PROCESSOR_REVISION: 3c03
USERNAME: SYSTEM
windir: C:\WINDOWS
PROCESSOR_IDENTIFIER: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
PSModulePath: C:\Program Files (x86)\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\PowerShell\Modules\
NUMBER_OF_PROCESSORS: 8
DriverData: C:\Windows\System32\Drivers\DriverData
PROCESSOR_LEVEL: 6
Path: C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files (x86)\Tencent\微信web开发者工具\dll;C:\Program Files\dotnet\
TMP: C:\WINDOWS\TEMP
PATHEXT: .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
用户环境变量:
TEMP: C:\Users\Administrator\AppData\Local\Temp
Path: C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\.dotnet\tools
TMP: C:\Users\Administrator\AppData\Local\Temp
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
进程环境变量:
PROCESSOR_ARCHITEW6432: AMD64
DriverData: C:\Windows\System32\Drivers\DriverData
COMPUTERNAME: WIN-SHENGWU
CommonProgramFiles(x86): C:\Program Files (x86)\Common Files
HOMEPATH: \Users\Administrator
VisualStudioEdition: Microsoft Visual Studio Professional 2022
ServiceHubLogSessionKey: B6FC3BDB
PROCESSOR_REVISION: 3c03
ThreadedWaitDialogDpiContext: -4
PkgDefApplicationConfigFile: C:\Users\Administrator\AppData\Local\Microsoft\VisualStudio\17.0_957707bc\devenv.exe.config
PATHEXT: .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
TMP: C:\Users\ADMINI~1\AppData\Local\Temp
TEMP: C:\Users\ADMINI~1\AppData\Local\Temp
LOCALAPPDATA: C:\Users\Administrator\AppData\Local
PUBLIC: C:\Users\Public
VSAPPIDNAME: devenv.exe
USERDOMAIN: WIN-SHENGWU
ProgramFiles(x86): C:\Program Files (x86)
COMPLUS_NoGuiFromShim: 1
PROCESSOR_LEVEL: 6
PROCESSOR_IDENTIFIER: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
_NO_DEBUG_HEAP: 1
PSModulePath: C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\PowerShell\Modules\
NUMBER_OF_PROCESSORS: 8
FPS_BROWSER_USER_PROFILE_STRING: Default
CommonProgramFiles: C:\Program Files (x86)\Common Files
VisualStudioDir: C:\Users\Administrator\Documents\Visual Studio 2022
ProgramData: C:\ProgramData
ProgramW6432: C:\Program Files
ProgramFiles: C:\Program Files (x86)
ENABLE_XAML_DIAGNOSTICS_SOURCE_INFO: 1
VSSKUEDITION: Professional
SystemRoot: C:\WINDOWS
VSAPPIDDIR: C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\
SESSIONNAME: Console
VisualStudioVersion: 17.0
CommonProgramW6432: C:\Program Files\Common Files
LOGONSERVER: \\WIN-SHENGWU
Path: C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files (x86)\Tencent\微信web开发者工具\dll;C:\Program Files\dotnet\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\.dotnet\tools
USERPROFILE: C:\Users\Administrator
MSBuildLoadMicrosoftTargetsReadOnly: true
VSLANG: 2052
USERDOMAIN_ROAMINGPROFILE: WIN-SHENGWU
APPDATA: C:\Users\Administrator\AppData\Roaming
HOMEDRIVE: C:
USERNAME: Administrator
FPS_BROWSER_APP_PROFILE_STRING: Internet Explorer
PROCESSOR_ARCHITECTURE: x86
OS: Windows_NT
ComSpec: C:\WINDOWS\system32\cmd.exe
SystemDrive: C:
windir: C:\WINDOWS
ALLUSERSPROFILE: C:\ProgramData
图片展示效果: