-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetSystemInfo(win).cs
177 lines (159 loc) · 5.34 KB
/
GetSystemInfo(win).cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System.Diagnostics;
using System.Management;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
public static class HardwareInfo
{
public static string UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
public static string GetAccountName()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_UserAccount");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Name").ToString();
}
catch { }
}
return "User Account Name: Unknown";
}
public static string GetPhysicalMemory()
{
ManagementScope oMs = new ManagementScope();
ObjectQuery oQuery = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
ManagementObjectCollection oCollection = oSearcher.Get();
long MemSize = 0;
long mCap = 0;
foreach (ManagementObject obj in oCollection)
{
mCap = Convert.ToInt64(obj["Capacity"]);
MemSize += mCap;
}
MemSize = (MemSize / 1024) / 1024;
return MemSize.ToString() + "MB";
}
public static int GetCPUCurrentClockSpeed()
{
int cpuClockSpeed = 0;
ManagementClass mgmt = new ManagementClass("Win32_Processor");
ManagementObjectCollection objCol = mgmt.GetInstances();
foreach (ManagementObject obj in objCol)
{
if (cpuClockSpeed == 0)
{
cpuClockSpeed = Convert.ToInt32(obj.Properties["CurrentClockSpeed"].Value.ToString());
}
}
return cpuClockSpeed;
}
public static double? GetCpuSpeedInGHz()
{
double? GHz = null;
using (ManagementClass mc = new ManagementClass("Win32_Processor"))
{
foreach (ManagementObject mo in mc.GetInstances())
{
GHz = 0.001 * (UInt32)mo.Properties["CurrentClockSpeed"].Value;
break;
}
}
return GHz;
}
public static string GetOSInformation()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return ((string)wmi["Caption"]).Trim() + ", " + (string)wmi["Version"] + ", " + (string)wmi["OSArchitecture"];
}
catch { }
}
return "BIOS Maker: Unknown";
}
public static String GetProcessorInformation()
{
ManagementClass mc = new ManagementClass("win32_processor");
ManagementObjectCollection moc = mc.GetInstances();
String name = String.Empty;
foreach (ManagementObject mo in moc)
{
name = (string)mo["Name"];
// old style
// name = name.Replace("(TM)", "™").Replace("(tm)", "™").Replace("(R)", "®").Replace("(r)", "®").Replace("(C)", "©").Replace("(c)", "©").Replace(" ", " ").Replace(" ", " ");
name = name.Replace("(TM)", "").Replace("(tm)", "").Replace("(R)", "").Replace("(r)", "").Replace("(C)", "").Replace("(c)", "").Replace(" ", " ").Replace(" ", " ").Replace("@", "").Replace("CPU", "");
}
return name;
}
public static String GetComputerName()
{
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
String info = String.Empty;
foreach (ManagementObject mo in moc)
{
info = (string)mo["Name"];
}
return info;
}
public static string GetSystemUpTimeInfo()
{
try
{
var time = GetSystemUpTime();
var upTime = string.Format("{0:D2}h:{1:D2}m:{2:D2}s", time.Hours, time.Minutes, time.Seconds);
return string.Format("{0}", upTime);
}
catch (Exception)
{
return string.Empty;
}
}
private static TimeSpan GetSystemUpTime()
{
try
{
using var uptime = new PerformanceCounter("System", "System Up Time");
uptime.NextValue();
return TimeSpan.FromSeconds(uptime.NextValue());
}
catch (Exception)
{
return new TimeSpan(0, 0, 0, 0);
}
}
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
public static string GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
public static string PingTest()
{
Ping myPing = new Ping();
string result = string.Empty;
try
{
PingReply reply = myPing.Send("google.com", 500);
result = reply.RoundtripTime.ToString();
if(reply.RoundtripTime > 65) result += " [Bad internet ⚠]";
}
catch{
result = "Error";
}
return result;
}
}