How to find various machine-level system information with C# .NET
January 13, 2017 Leave a comment
The Environment class holds a range of properties that help you describe the system your app is running on. Here come some examples with inline comments:
//returns true on my PC as it is a 64-bit OS bool is64BitOperatingSystem = Environment.Is64BitOperatingSystem; //returns the machine name, in my case ANDRAS1 string machineName = Environment.MachineName; //returns information about the operating system version, build, major, minor etc. OperatingSystem os = Environment.OSVersion; //returns the platform id as an enumeration, in my case it's Win32NT PlatformID platform = os.Platform; //the currently installed service pack, Service Pack 1 in my case string servicePack = os.ServicePack; //the toString version of the OS, this is "Microsoft Windows NT 6.1.7601 Service Pack 1" on this PC string version = os.VersionString; //I have 4 processors on this PC int processorCount = Environment.ProcessorCount; //returns 2 logical drives: C: and D: string[] logicalDrives = Environment.GetLogicalDrives(); //this is how to find all environmental variables of the system and iterate through them IDictionary envVars = Environment.GetEnvironmentVariables(); foreach (string key in envVars.Keys) { //e.g. the JAVA_HOME env.var is set to "C:\Progra~1\Java\jdk1.7.0_51\" Debug.WriteLine(string.Concat("key: ", key, ": ", envVars[key])); } //retrieve the current CLR version, in my case it's "4.0.30319.18444" Version clrVersion = Environment.Version;
View all posts related to diagnostics here.