вторник, 12 октября 2010 г.

Информация о системе в .Net

Этот пост скорее просто памятка .Net программистам о том, как получать некоторые важные свойства системы - версию Runtime, 64 или 32-битная система, сколько используется процессоров и пр. Все эти проверки можно найти в интернете или копанием в классах вроде System.Environment, но лично мне удобно держать все в одном вспомогательном классе.

Класс целиком из ядра RunServer:

/// <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);
}
}