System Information in .Net
This post is more of a cheat sheet for .Net developers on how to get some important system properties — the runtime version, whether the system is 64-bit or 32-bit, how many processors are in use, and so on. You can find all these checks online, or dig them up in classes like System.Environment, but personally I find it convenient to keep them all in one helper class.
The full class from the RunServer core:
/// <summary> /// System information class /// </summary> public static class SystemInfo { /// <summary> /// Is there Mono Runtime available? /// </summary> public static readonly bool IsMono = Type.GetType("Mono.Runtime") != null; /// <summary> /// Are we on x86 architecture? /// </summary> public static readonly bool IsX86 = IntPtr.Size == 4; /// <summary> /// Are we on x64 architecture? /// </summary> public static readonly bool IsX64 = IntPtr.Size == 8; /// <summary> /// Are we using Server Garbage Collector? /// </summary> public static readonly bool IsServerGC = GCSettings.IsServerGC; /// <summary> /// Is there console windows attached or we are running in /// service/GUI app? /// </summary> public static readonly bool IsConsole = Console.OpenStandardOutput() != Stream.Null; /// <summary> /// Assembly was compiled in Debug mode? /// </summary> public static readonly bool IsDebug = #if DEBUG true; #else false; #endif /// <summary> /// Runtime version /// </summary> public static readonly Version RuntimeVersion = Environment.Version; /// <summary> /// Operating System version /// </summary> public static readonly Version OSVersion = Environment.OSVersion.Version; /// <summary> /// CPU count /// </summary> public static readonly int ProcessorCount = Environment.ProcessorCount; /// <summary> /// Dumps system information the specified stream. /// </summary> /// <param name="stream">Output stream</param> public static void Dump(StreamWriter stream) { stream.WriteLine("Is Mono: {0}", IsMono); stream.WriteLine("Is x86: {0}", IsX86); stream.WriteLine("Is x64: {0}", IsX64); stream.WriteLine("Is ServerGC: {0}", IsServerGC); stream.WriteLine("Is Console: {0}", IsConsole); stream.WriteLine("Is Debug: {0}", IsDebug); stream.WriteLine("Runtime Version: {0}", RuntimeVersion); stream.WriteLine("OS Version: {0}", OSVersion); stream.WriteLine("CPU Count: {0}", ProcessorCount); } }
Originally published 2010-10-12.
