[转][C#]Environment 类
当执行 Environment.GetEnvironmentVariables() 时,可以得到以下结果(受所安装软件影响,每台电脑都不一样)
Count = 53 ["SystemDrive"]: "C:" ["ProgramFiles(x86)"]: "C:\\Program Files (x86)" ["ProgramW6432"]: "C:\\Program Files" ["PROCESSOR_IDENTIFIER"]: "Intel64 Family 6 Model 60 Stepping 3, GenuineIntel" ["TMP"]: "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp" ["PROCESSOR_ARCHITECTURE"]: "AMD64" ["PATHEXT"]: ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC" ["VisualStudioVersion"]: "15.0" ["COMPUTERNAME"]: "C-123" ["PkgDefApplicationConfigFile"]: "C:\\Users\\Administrator\\AppData\\Local\\Microsoft\\VisualStudio\\15.0_b81\\devenv.exe.config" ["SESSIONNAME"]: "Console" ["PROCESSOR_REVISION"]: "3c03" ["TEMP"]: "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp" ["ENABLE_XAML_DIAGNOSTICS_SOURCE_INFO"]: "1" ["FPS_BROWSER_USER_PROFILE_STRING"]: "Default" ["LOGONSERVER"]: "\\\\C-123" ["USERNAME"]: "Administrator" ["SystemRoot"]: "C:\\windows" ["VSSKUEDITION"]: "Community" ["USERPROFILE"]: "C:\\Users\\Administrator" ["FPS_BROWSER_APP_PROFILE_STRING"]: "Internet Explorer" ["XAMARIN_ANDROID_REGKEY"]: "SOFTWARE\\Xamarin\\VisualStudio\\15.0_b81\\Android" ["OneDrive"]: "C:\\Users\\Administrator\\OneDrive" ["CommonProgramFiles"]: "C:\\Program Files\\Common Files" ["ProgramData"]: "C:\\ProgramData" ["VSAPPIDDIR"]: "C:\\Program Files\\Microsoft Visual Studio\\2017\\Community\\Common7\\IDE\\" ["HOMEPATH"]: "\\Users\\Administrator" ["MONO_ANDROID_PATH"]: "C:\\Program Files\\Microsoft Visual Studio\\2017\\Community\\MSBuild\\Xamarin\\Android" ["ALLUSERSPROFILE"]: "C:\\ProgramData" ["CommonProgramW6432"]: "C:\\Program Files\\Common Files" ["VisualStudioEdition"]: "Microsoft Visual Studio Community 2017" ["VSLANG"]: "2052" ["Path"]: "C:\\Program Files\\Java\\jdk1.8.0_181\\bin;C:\\windows\\system32;C:\\windows;C:\\windows\\System32\\Wbem;C:\\windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\dotnet\\;C:\\Program Files\\Microsoft SQL Server\\130\\Tools\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\100\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\100\\DTS\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\VSShell\\Common7\\IDE\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\DTS\\Binn\\;C:\\Users\\Administrator\\AppData\\Local\\Microsoft\\WindowsApps;" ["HOMEDRIVE"]: "C:" ["windir"]: "C:\\windows" ["NUMBER_OF_PROCESSORS"]: "4" ["OS"]: "Windows_NT" ["CommonProgramFiles(x86)"]: "C:\\Program Files (x86)\\Common Files" ["ProgramFiles"]: "C:\\Program Files" ["ComSpec"]: "C:\\windows\\system32\\cmd.exe" ["COMPLUS_NoGuiFromShim"]: "1" ["JAVA_HOME"]: "C:\\Program Files\\Java\\jdk1.8.0_181" ["PSModulePath"]: "C:\\Program Files\\WindowsPowerShell\\Modules;C:\\windows\\system32\\WindowsPowerShell\\v1.0\\Modules" ["VisualStudioDir"]: "C:\\Users\\Administrator\\Documents\\Visual Studio 2017" ["APPDATA"]: "C:\\Users\\Administrator\\AppData\\Roaming" ["USERDOMAIN"]: "C-123" ["PROCESSOR_LEVEL"]: "6" ["LOCALAPPDATA"]: "C:\\Users\\Administrator\\AppData\\Local" ["USERDOMAIN_ROAMINGPROFILE"]: "C-123" ["VSIDE"]: "true" ["PUBLIC"]: "C:\\Users\\Public" ["VSAPPIDNAME"]: "devenv.exe" ["MSBuildLoadMicrosoftTargetsReadOnly"]: "true"
或者这样取值:
Environment.UserDomainName // 获取与当前用户关联的网络域名。
Environment.ProcessorCount // 获取当前计算机上的处理器数。
Environment.WorkingSet // 获取映射到进程上下文的物理内存量。
Environment.Version // 获取公共语言运行库的版本信息。
Environment.OSVersion // 获取系统版本号,类似于 Microsoft Windows NT 6.2.9200.0
Environment.GetLogicalDrives() // 返回当前计算机中的逻辑驱动器名称(数组)。
Environment.GetFolderPath(Environment.SpecialFolder.Desktop) // 获取桌面路径(逻辑)。
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) // 获取桌面路径(物理)。
获取当前操作系统版本:(Win 7、Win 8、Win 10 待完善)
/// <summary> /// 获取当前运行的操作系统版本。 /// </summary> /// <returns><see cref="Platform"/> 的值之一,它表示当前运行的操作系统版本。</returns> private static Platform GetCurrentPlatform() { OperatingSystem os = Environment.OSVersion; Platform pt; switch(os.Platform) { case (PlatformID.Win32Windows): // Win95, Win98 or Me switch(os.Version.Minor) { case (0): // 95 pt = Platform.Windows95; break; case (10): // 98 if(os.Version.Revision.ToString() == "2222A") pt = Platform.Windows982ndEdition; else pt = Platform.Windows98; break; case (90): // winme pt = Platform.WindowsME; break; default: // Unknown pt = Platform.UnKnown; break; } break; case (PlatformID.Win32NT): //Win2k or Xp or 2003 switch(os.Version.Major) { case (3): pt = Platform.WindowsNT351; break; case (4): pt = Platform.WindowsNT40; break; case (5): if(os.Version.Minor == 0) pt = Platform.Windows2000; else if(os.Version.Minor == 1) pt = Platform.WindowsXP; else if(os.Version.Minor == 2) pt = Platform.Windows2003; else pt = Platform.UnKnown; break; case (6): pt = Platform.WindowsVista; break; default: pt = Platform.UnKnown; break; } break; case (PlatformID.WinCE): // WinCE pt = Platform.WindowsCE; break; case (PlatformID.Win32S): case (PlatformID.Unix): default: pt = Platform.UnKnown; break; } return pt; } /// <summary> /// 表示操作系统平台。 /// </summary> private enum Platform : byte { /// <summary> /// Windows 95 操作系统. /// </summary> Windows95, /// <summary> /// Windows 98 操作系统. /// </summary> Windows98, /// <summary> /// Windows 98 第二版操作系统. /// </summary> Windows982ndEdition, /// <summary> /// Windows ME 操作系统. /// </summary> WindowsME, /// <summary> /// Windows NT 3.51 操作系统. /// </summary> WindowsNT351, /// <summary> /// Windows NT 4.0 操作系统. /// </summary> WindowsNT40, /// <summary> /// Windows 2000 操作系统. /// </summary> Windows2000, /// <summary> /// Windows XP 操作系统. /// </summary> WindowsXP, /// <summary> /// Windows 2003 操作系统. /// </summary> Windows2003, /// <summary> /// Windows Vista 操作系统. /// </summary> WindowsVista, /// <summary> /// Windows CE 操作系统. /// </summary> WindowsCE, /// <summary> /// 操作系统版本未知。 /// </summary> UnKnown }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步