Advertisement
Guest User

Untitled

a guest
Oct 4th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 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.  
  7. namespace Test_Form
  8. {
  9.     class Manager
  10.     {
  11.         public TimeSpan uptime { get; set; }
  12.         public string host { get; set; }
  13.         // Q: Add new attributes and init they in constructor
  14.  
  15.         public Manager(string host, string username = null, string password = null)
  16.         {
  17.             ManagementObject mgnobj = GetServerInfo(host, username, password);
  18.  
  19.             this.host = host;
  20.  
  21.             if (mgnobj != null)
  22.             {
  23.                 ulong iUptimeInSec = ((ulong) mgnobj["Timestamp_Object"] - (ulong)(mgnobj["SystemUpTime"])) / (ulong) mgnobj["Frequency_Object"];
  24.                 double sUptime = Convert.ToDouble(iUptimeInSec);
  25.                 this.uptime = TimeSpan.FromSeconds(sUptime);
  26.  
  27.                 // Q: init other memeber attributes from mgnobj
  28.             }
  29.         }
  30.  
  31.         public ManagementObject GetServerInfo(string host, string username = null, string password = null)
  32.         {
  33.             ConnectionOptions options = new ConnectionOptions();
  34.             if (username != null)
  35.             {
  36.                 options.Username = username;
  37.             }
  38.  
  39.             if (password != null)
  40.             {
  41.                 options.Password = password;
  42.             }
  43.  
  44.             ManagementScope scope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", host), options);
  45.  
  46.             try
  47.             {
  48.                 options.Authentication = AuthenticationLevel.Packet;
  49.                 scope.Connect();
  50.             }
  51.             catch (Exception e) { }
  52.  
  53.             if (!scope.IsConnected)
  54.             {
  55.                 // Change the AuthenticationLevel for windows 2000 of lower
  56.                 try
  57.                 {
  58.                     options.Authentication = AuthenticationLevel.Connect;
  59.                     scope.Connect();
  60.                 }
  61.                 catch (Exception e) { }
  62.             }
  63.  
  64.             ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_PerfRawData_PerfOS_System");
  65.             ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
  66.  
  67.             ManagementObjectCollection queryCollection = searcher.Get();
  68.  
  69.             if (queryCollection.Count <= 0)
  70.             {
  71.                 return null;
  72.             }
  73.  
  74.             foreach (ManagementObject m in queryCollection)
  75.             {
  76.                 foreach (PropertyData prop in m.Properties)
  77.                 {
  78.                     Console.WriteLine("{0}: {1}", prop.Name, prop.Value);
  79.                 }
  80.                 return m;
  81.             }
  82.  
  83.             return null;
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement