-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemStateInfo.cs
More file actions
357 lines (318 loc) · 12.4 KB
/
SystemStateInfo.cs
File metadata and controls
357 lines (318 loc) · 12.4 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using Microsoft.Win32;
namespace Auto_Statistic
{
public static class SystemStateInfo
{
private static readonly PerformanceCounter availableRamSize = new PerformanceCounter("Memory", "Available MBytes");
private static readonly PerformanceCounter totalCpuUsage = new PerformanceCounter("Process", "% Processor Time", "_Total");
private static readonly PerformanceCounter idleCpuUsage = new PerformanceCounter("Process", "% Processor Time", "Idle");
public static float AvailableRamSize()
{
return availableRamSize.NextValue();
}
public static float TotalCpuUsage()
{
return totalCpuUsage.NextValue();
}
public static float IdleCpuUsage()
{
return idleCpuUsage.NextValue();
}
}
public static class FullSystemInfo
{
private static List<CpuInfo> CpuInfoList = null;
private static void getCpuInfo()
{
List<string> properties = new List<string>()
{
"Name",
"NumberOfCores",
"NumberOfLogicalProcessors",
"MaxClockSpeed",
"AddressWidth",
"DataWidth",
"L2CacheSize",
"L3CacheSize",
"L4CacheSize"
};
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
CpuInfoList = new List<CpuInfo>();
foreach (var queryObj in searcher.Get())
{
Dictionary<string, object> info = new Dictionary<string, object>();
foreach (var prop in properties)
{
try
{
info.Add(prop,queryObj[prop]);
}
catch (Exception exc)
{
info.Add(prop, 0);
}
}
CpuInfoList.Add(new CpuInfo(
info["Name"].ToString(),
Convert.ToInt32(info["NumberOfCores"]),
Convert.ToInt32(info["NumberOfLogicalProcessors"]),
Convert.ToInt32(info["MaxClockSpeed"]),
Convert.ToInt32(info["AddressWidth"]),
Convert.ToInt32(info["DataWidth"]),
Convert.ToInt32(info["L2CacheSize"]),
Convert.ToInt32(info["L3CacheSize"]),
Convert.ToInt32(info["L4CacheSize"])
));
}
}
public struct CpuInfo
{
public readonly string Name;
public readonly int NumCores;
public readonly int NumInstrThreads;
public readonly int BaseFrequency;
public readonly int L1InstrCacheSize;
public readonly int L1DataCacheSize;
public readonly int L2CacheSize;
public readonly int L3CacheSize;
public readonly int L4CacheSize;
public CpuInfo(string cpuName, int numberOfCpuCores, int numberOfCpuInstructionThreads, int cpuBaseFrequency, int cpuL1InstructionCacheSize, int cpuL1DataCacheSize, int cpuL2CacheSize, int cpuL3CacheSize, int cpuL4CacheSize)
{
Name = cpuName;
NumCores = numberOfCpuCores;
NumInstrThreads = numberOfCpuInstructionThreads;
BaseFrequency = cpuBaseFrequency;
L1InstrCacheSize = cpuL1InstructionCacheSize;
L1DataCacheSize = cpuL1DataCacheSize;
L2CacheSize = cpuL2CacheSize;
L3CacheSize = cpuL3CacheSize;
L4CacheSize = cpuL4CacheSize;
}
public Dictionary<string, string> ToDictionary()
{
var info = new Dictionary<string, string>();
foreach (var field in typeof(CpuInfo).GetFields())
{
info.Add(field.Name, field.GetValue(this).ToString());
}
return info;
}
}
public static List<CpuInfo> CPUsInfo
{
get
{
if (CpuInfoList == null) getCpuInfo();
return CpuInfoList;
}
}
//-------------------------------------------------------
/// <summary>
/// Returns the total amount of computer RAM in KBytes.
/// </summary>
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetPhysicallyInstalledSystemMemory(out long totalMemoryInKilobytes);
private static List<RamBoardInfo> RamBoardInfoList = null;
private static RamInfo ramInfo = null;
private static readonly Dictionary<int, string> RamTypes = new Dictionary<int, string>()
{
{0, "Unknown (0)"},
{1, "Other (1)"},
{2, "DRAM"},
{3, "Synchronous DRAM"},
{4, "Cache DRAM"},
{5, "EDO"},
{6, "EDRAM"},
{7, "VRAM"},
{8, "SRAM"},
{9, "RAM"},
{10, "ROM"},
{11, "Flash"},
{12, "EEPROM"},
{13, "FEPROM"},
{14, "EPROM"},
{15, "CDRAM"},
{16, "3DRAM"},
{17, "SDRAM"},
{18, "SGRAM"},
{19, "RDRAM"},
{20, "DDR"},
{21, "DDR2"},
{22, "DDR2 FB-DIMM"},
{24, "DDR3"},
{25, "FBD2"},
{26, "DDR4"}
};
private static void getRamInfo()
{
RamBoardInfoList = new List<RamBoardInfo>();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");
int ramFrequency = 0;
string ramType = "";
foreach (var queryObj in searcher.Get())
{
var ramBoard = new RamBoardInfo(queryObj["PartNumber"].ToString(), Convert.ToInt64(queryObj["Capacity"]) / 1024 / 1024);
ramFrequency = Convert.ToInt32(queryObj["Speed"]);
ramType = RamTypeToString(Convert.ToInt32(queryObj["MemoryType"]));
RamBoardInfoList.Add(ramBoard);
}
GetPhysicallyInstalledSystemMemory(out long totalRamSize);
totalRamSize /= 1024;
ramInfo = new RamInfo(totalRamSize, ramType, ramFrequency, 0);
}
private static string RamTypeToString(int type)
{
string outValue = null;
if (RamTypes.ContainsKey(type))
outValue = RamTypes[type];
else
outValue = "Undefined (" + type.ToString() + ")";
return outValue;
}
public class RamInfo
{
public readonly long totalRamSizeMB;
public readonly string ramType;
public readonly int ramFrequency;
public readonly int numberOfRamChannels;
public RamInfo(long totalRamSizeMb, string ramType, int ramFrequency, int numberOfRamChannels)
{
this.totalRamSizeMB = totalRamSizeMb;
this.ramType = ramType;
this.ramFrequency = ramFrequency;
this.numberOfRamChannels = numberOfRamChannels;
}
public Dictionary<string, string> ToDictionary()
{
var info = new Dictionary<string, string>();
foreach (var field in typeof(RamInfo).GetFields())
{
info.Add(field.Name, field.GetValue(this).ToString());
}
return info;
}
}
public struct RamBoardInfo
{
public RamBoardInfo(string name, long size)
{
Name = name;
Size = size;
}
public readonly string Name;
public readonly long Size;
public Dictionary<string, string> ToDictionary()
{
var info = new Dictionary<string, string>();
foreach (var field in typeof(RamBoardInfo).GetFields())
{
info.Add(field.Name, field.GetValue(this).ToString());
}
return info;
}
}
public static List<RamBoardInfo> RamBoardsInfo
{
get
{
if (RamBoardInfoList == null) getRamInfo();
return RamBoardInfoList;
}
}
public static RamInfo GeneralRamInfo
{
get
{
if (ramInfo == null) getRamInfo();
return ramInfo;
}
}
//-------------------------------------------------------------------
public static class OsInfo
{
public static string Name = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName", "").ToString();
public static int BitDepth = Environment.Is64BitOperatingSystem ? 64 : 32;
public static string Version = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ReleaseId", "").ToString();
public static string BuildVersion = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion",
"CurrentBuild", "").ToString();
public static Dictionary<string, string> ToDictionary()
{
var info = new Dictionary<string, string>();
foreach (var field in typeof(OsInfo).GetFields())
{
info.Add(field.Name, field.GetValue(null).ToString());
}
return info;
}
}
/*public static Dictionary<string, string> BiosInfo()
{
List<string> properties = new List<string>() { };
Dictionary<string, string> result = new Dictionary<string, string>(properties.Count);
string query = "SELECT * FROM Win32_BaseBoard";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject info in searcher.Get())
{
foreach (var property in properties)
{
result[property] = info.GetPropertyValue(property).ToString();
}
}
return result;
}*/
public static Dictionary<string, Dictionary<string, string>> Get()
{
var info = new Dictionary<string, Dictionary<string, string>>();
info.Add("OS", OsInfo.ToDictionary());
var i = 1;
foreach (var cpuInfo in CPUsInfo)
{
info.Add("CPU " + i, cpuInfo.ToDictionary());
++i;
}
info.Add("RAM", GeneralRamInfo.ToDictionary());
i = 1;
foreach (var ramBoard in RamBoardsInfo)
{
info.Add("RAM board " + i, ramBoard.ToDictionary());
++i;
}
/*i = 1;
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_CacheMemory");
foreach (var obj in searcher.Get())
{
Dictionary<string, string> result = new Dictionary<string, string>(obj.Properties.Count);
foreach (var property in obj.Properties)
{
if (property.Value != null)
result[property.Name] = property.Value.ToString();
}
info.Add("Win32_CacheMemory " + i, result);
++i;
}*/
return info;
}
public static void WriteFullSystemInfo(string path)
{
if (File.Exists(path)) File.SetAttributes(path, FileAttributes.Normal);
using (StreamWriter sysInfoFS = new StreamWriter(File.Create(path), Encoding.GetEncoding(1251)))
foreach (var infoType in Get())
{
sysInfoFS.WriteLine(infoType.Key + ";;");
foreach (var info in infoType.Value)
sysInfoFS.WriteLine(";" + info.Key + ";" + info.Value);
}
}
}
}