Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Management;
- namespace Test_Form
- {
- class Manager
- {
- public TimeSpan uptime { get; set; }
- public string host { get; set; }
- // Q: Add new attributes and init they in constructor
- public Manager(string host, string username = null, string password = null)
- {
- ManagementObject mgnobj = GetServerInfo(host, username, password);
- this.host = host;
- if (mgnobj != null)
- {
- ulong iUptimeInSec = ((ulong) mgnobj["Timestamp_Object"] - (ulong)(mgnobj["SystemUpTime"])) / (ulong) mgnobj["Frequency_Object"];
- double sUptime = Convert.ToDouble(iUptimeInSec);
- this.uptime = TimeSpan.FromSeconds(sUptime);
- // Q: init other memeber attributes from mgnobj
- }
- }
- public ManagementObject GetServerInfo(string host, string username = null, string password = null)
- {
- ConnectionOptions options = new ConnectionOptions();
- if (username != null)
- {
- options.Username = username;
- }
- if (password != null)
- {
- options.Password = password;
- }
- ManagementScope scope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", host), options);
- try
- {
- options.Authentication = AuthenticationLevel.Packet;
- scope.Connect();
- }
- catch (Exception e) { }
- if (!scope.IsConnected)
- {
- // Change the AuthenticationLevel for windows 2000 of lower
- try
- {
- options.Authentication = AuthenticationLevel.Connect;
- scope.Connect();
- }
- catch (Exception e) { }
- }
- ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_PerfRawData_PerfOS_System");
- ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
- ManagementObjectCollection queryCollection = searcher.Get();
- if (queryCollection.Count <= 0)
- {
- return null;
- }
- foreach (ManagementObject m in queryCollection)
- {
- foreach (PropertyData prop in m.Properties)
- {
- Console.WriteLine("{0}: {1}", prop.Name, prop.Value);
- }
- return m;
- }
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement