Advertisement
konalisp

Get OS/Runtime

Aug 4th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. using System.Management; //.NET
  3. using System.Diagnostics; //Mono
  4.  
  5. class TestClass {
  6.    
  7.     public static bool IsRunningOnMono () {
  8.         return Type.GetType ("Mono.Runtime") != null;
  9.     }
  10.    
  11.     static int Main( string[] args ) {
  12.         OperatingSystem os = System.Environment.OSVersion;
  13.         Console.WriteLine("Machine: {0}", Environment.MachineName);
  14.         Console.WriteLine("Platform: {0}", os.Platform);
  15.         if (os.Platform == PlatformID.Unix) {
  16.             Console.WriteLine("Total Memory: {0:N0} MB", CountPhysicalMemory());
  17.         }
  18.         else if (os.Platform == PlatformID.Win32NT) {
  19.             Console.WriteLine("Total Memory: {0:N0} MB", CountPhysicalMemory());
  20.             //this is completely redundant but keeping it anyway as
  21.             //a reference for how PlatformID works.
  22.         }
  23.         else {
  24.             Console.WriteLine("Platform: No idea what OS this is lol.");
  25.         }
  26.         Console.WriteLine("\nCLR Version: {0}", System.Environment.Version);
  27.         Console.WriteLine("Thanks!");
  28.         string o = Console.ReadLine();
  29.         return 0;
  30.     }
  31.    
  32.    
  33.     private static UInt64 CountPhysicalMemory(){
  34.         OperatingSystem os = System.Environment.OSVersion;
  35.         if (!IsRunningOnMono()) {
  36.                 //.NET only
  37.             ManagementObjectSearcher objects =
  38.                 new ManagementObjectSearcher(
  39.                 "SELECT * FROM Win32_PhysicalMemory");
  40.             ManagementObjectCollection coll = objects.Get();
  41.             UInt64 total = 0;
  42.             foreach (ManagementObject obj in coll) {
  43.                 total += (UInt64)obj["Capacity"];
  44.             }
  45.             return total/1024/1024;
  46.         }
  47.         else {
  48.             //Mono only
  49.             var pc = new PerformanceCounter ("Mono Memory", "Total Physical Memory");
  50.             return (UInt64)pc.RawValue/1024/1024;
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement