David_Kassa

System Iformation class

Oct 3rd, 2012
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Management;
  6. using System.IO;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10.     /// <summary>
  11.     /// version 1.0
  12.     /// created by David Kassa
  13.     /// </summary>
  14.  
  15.     public class SystemInfo
  16.     {
  17.  
  18.         #region Memorys
  19.  
  20.         static public string RAM
  21.         {
  22.             get {return GetRam();}
  23.         }
  24.  
  25.  
  26.         private static string GetRam()
  27.         {
  28.             try
  29.             {
  30.                 ManagementScope oMs = new ManagementScope();
  31.                 ObjectQuery oQuery = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
  32.                 ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
  33.                 ManagementObjectCollection oReturnCollection = oSearcher.Get();
  34.                 int ammount = 0;
  35.                 foreach (ManagementObject oReturn in oReturnCollection)
  36.                 {
  37.                     object hoi = oReturn["Capacity"];
  38.                     long temp = Convert.ToInt64(oReturn["Capacity"]) / 1024 / 1024 / 1024;
  39.                     ammount += Convert.ToInt32(temp);
  40.                 }
  41.                 oMs = null;
  42.                 oQuery = null;
  43.                 oSearcher = null;
  44.                 oReturnCollection = null;
  45.                 return ammount.ToString() + " GB";
  46.                
  47.             }
  48.             catch (Exception)
  49.             {
  50.                 return "Not Found";
  51.             }        
  52.         }
  53.  
  54.         public static string GetDrivsInfo(string lang)
  55.         {
  56.             string discsInfo = null;
  57.             DriveInfo[] allDrives = DriveInfo.GetDrives();
  58.             if (lang == "he" || lang == "heb" || lang == "Hebrew")
  59.             {
  60.                 foreach (DriveInfo d in allDrives)
  61.                     if (d.IsReady == true)
  62.                         discsInfo = discsInfo + " כונן " + d.Name + " - שטח זמין: " + d.TotalFreeSpace / 1000 / 1000 / 1000 + " GB," + " קיבולת: " + d.TotalSize / 1000 / 1000 / 1000 + " GB\n";
  63.             }
  64.             else
  65.             {
  66.                 foreach (DriveInfo d in allDrives)
  67.                     if (d.IsReady == true)
  68.                         discsInfo = discsInfo + " Drive " + d.Name + " - Available space: " + d.TotalFreeSpace / 1000 / 1000 / 1000 + " GB," + " Total size of drive: " + d.TotalSize / 1000 / 1000 / 1000 + " GB\n";
  69.             }
  70.             return discsInfo;
  71.         }
  72.  
  73.         #endregion
  74.  
  75.  
  76.         #region CPU
  77.  
  78.  
  79.         public static int CountProcessors()
  80.         {
  81.             return Environment.ProcessorCount;
  82.         }
  83.  
  84.         private static uint getCPU32()
  85.         {
  86.             ManagementObject Mo = new ManagementObject("Win32_Processor.DeviceID='CPU0'");
  87.             uint sp = (uint)(Mo["CurrentClockSpeed"]);
  88.             Mo.Dispose();
  89.             return sp;
  90.         }
  91.         private static uint getCPU64()
  92.         {
  93.             ManagementObject Mo = new ManagementObject("Win64_Processor.DeviceID='CPU0'");
  94.             uint sp = (uint)(Mo["CurrentClockSpeed"]);
  95.             Mo.Dispose();
  96.             return sp;
  97.         }
  98.  
  99.         public static string GetCPUSpead()
  100.         {
  101.             try
  102.             {
  103.                 try
  104.                 {
  105.                     return getCPU32().ToString() + "MHz";
  106.                 }
  107.                 catch (Exception)
  108.                 {
  109.                     Console.WriteLine(Environment.Version.ToString());
  110.                     return getCPU64().ToString() + "MHz";
  111.                 }
  112.                
  113.             }
  114.  
  115.             catch (Exception)
  116.             {
  117.                 return "Not found";
  118.             }
  119.  
  120.         }
  121.  
  122.         #endregion
  123.  
  124.  
  125.         #region OS
  126.  
  127.         public static string GetMachinName()
  128.         {
  129.             return Environment.MachineName;
  130.         }
  131.  
  132.         public static string GetUserName()
  133.         {
  134.             return Environment.UserName;
  135.         }
  136.  
  137.         public static string GetOSVersion()
  138.         {
  139.             if (Environment.Is64BitProcess == true)
  140.                 return Environment.OSVersion.ToString() + " 64Bit";
  141.             return Environment.OSVersion.ToString() + " 32Bit";
  142.         }
  143.  
  144.  
  145.         public static string GetOSName()
  146.         {
  147.             var name = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>()
  148.                         select x.GetPropertyValue("Caption")).First();
  149.             return name != null ? name.ToString() : "Unknown";
  150.         }
  151.  
  152.         #endregion
  153.  
  154.         #region Graphics
  155.         public static string GPU()
  156.         {
  157.             ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DisplayConfiguration");
  158.             string graphicsCard = string.Empty;
  159.             foreach (ManagementObject mo in searcher.Get())
  160.             {
  161.                 foreach (PropertyData property in mo.Properties)
  162.                 {
  163.                     if (property.Name == "Description")
  164.                     {
  165.                         graphicsCard = property.Value.ToString();
  166.                     }
  167.                 }
  168.             }
  169.             return graphicsCard;
  170.         }
  171.  
  172.  
  173.         #endregion        
  174.  
  175.         #region Security
  176.  
  177.         public static string GetAntiVirus()
  178.         {
  179.             string str = string.Empty;
  180.             ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"\\" + Environment.MachineName + @"\root\SecurityCenter2", "SELECT * FROM AntivirusProduct");
  181.             ManagementObjectCollection instances = searcher.Get();
  182.             foreach (ManagementObject queryObj in instances)
  183.             {
  184.                 str = queryObj["displayName"].ToString();
  185.             }
  186.             if (str == string.Empty)
  187.             {
  188.                 str = "Not found";
  189.             }
  190.             return str;
  191.         }
  192.  
  193.         public static string GetFirewall()
  194.         {
  195.             string str = string.Empty;
  196.             ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"\\" + Environment.MachineName + @"\root\SecurityCenter2", "SELECT * FROM FirewallProduct");
  197.             ManagementObjectCollection instances = searcher.Get();
  198.             foreach (ManagementObject queryObj in instances)
  199.             {
  200.                 str = queryObj["displayName"].ToString();
  201.             }
  202.             if (str == string.Empty)
  203.             {
  204.                 str = "Not found";
  205.             }
  206.             return str;
  207.         }
  208.  
  209.         #endregion
  210.  
  211.         #region Time
  212.  
  213.         public static string GetTimeZone()
  214.         {
  215.             return TimeZone.CurrentTimeZone.StandardName;
  216.         }
  217.        
  218.         public static string GetRightNowTime()
  219.         {
  220.             return DateTime.Now.ToString();
  221.         }
  222.  
  223.         public static string GetRightNowHour()
  224.         {
  225.             return DateTime.Now.Hour.ToString();
  226.         }
  227.  
  228.         public static string GetRightNowMinute()
  229.         {
  230.             return DateTime.Now.Minute.ToString();
  231.         }
  232.  
  233.         public static string GetRightNowSecond()
  234.         {
  235.             return DateTime.Now.Second.ToString();
  236.         }
  237.        
  238.         #endregion
  239.  
  240.  
  241.        
  242.     }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment