SystemInfo.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. using Masuit.Tools.Logging;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Management;
  8. using System.Net;
  9. using System.Net.NetworkInformation;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. namespace Masuit.Tools.Hardware
  13. {
  14. /// <summary>
  15. /// 硬件信息,部分功能需要C++支持
  16. /// </summary>
  17. public static partial class SystemInfo
  18. {
  19. #region 字段
  20. private const int GwHwndfirst = 0;
  21. private const int GwHwndnext = 2;
  22. private const int GwlStyle = -16;
  23. private const int WsVisible = 268435456;
  24. private const int WsBorder = 8388608;
  25. private static readonly PerformanceCounter PcCpuLoad; //CPU计数器
  26. private static readonly PerformanceCounter MemoryCounter = new PerformanceCounter();
  27. private static readonly PerformanceCounter CpuCounter = new PerformanceCounter();
  28. private static readonly PerformanceCounter DiskReadCounter = new PerformanceCounter();
  29. private static readonly PerformanceCounter DiskWriteCounter = new PerformanceCounter();
  30. private static readonly string[] InstanceNames;
  31. private static readonly PerformanceCounter[] NetRecvCounters;
  32. private static readonly PerformanceCounter[] NetSentCounters;
  33. #endregion
  34. #region 构造函数
  35. /// <summary>
  36. /// 静态构造函数
  37. /// </summary>
  38. static SystemInfo()
  39. {
  40. //初始化CPU计数器
  41. PcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total")
  42. {
  43. MachineName = "."
  44. };
  45. PcCpuLoad.NextValue();
  46. //CPU个数
  47. ProcessorCount = Environment.ProcessorCount;
  48. //获得物理内存
  49. try
  50. {
  51. using var mc = new ManagementClass("Win32_ComputerSystem");
  52. using var moc = mc.GetInstances();
  53. foreach (var mo in moc)
  54. {
  55. if (mo["TotalPhysicalMemory"] != null)
  56. {
  57. PhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString());
  58. }
  59. }
  60. var cat = new PerformanceCounterCategory("Network Interface");
  61. InstanceNames = cat.GetInstanceNames();
  62. NetRecvCounters = new PerformanceCounter[InstanceNames.Length];
  63. NetSentCounters = new PerformanceCounter[InstanceNames.Length];
  64. for (int i = 0; i < InstanceNames.Length; i++)
  65. {
  66. NetRecvCounters[i] = new PerformanceCounter();
  67. NetSentCounters[i] = new PerformanceCounter();
  68. }
  69. CompactFormat = false;
  70. }
  71. catch (Exception e)
  72. {
  73. LogManager.Error(e);
  74. }
  75. }
  76. #endregion
  77. private static bool CompactFormat { get; set; }
  78. #region CPU核心
  79. /// <summary>
  80. /// 获取CPU核心数
  81. /// </summary>
  82. public static int ProcessorCount { get; }
  83. #endregion
  84. #region CPU占用率
  85. /// <summary>
  86. /// 获取CPU占用率 %
  87. /// </summary>
  88. public static float CpuLoad => PcCpuLoad.NextValue();
  89. #endregion
  90. #region 可用内存
  91. /// <summary>
  92. /// 获取可用内存
  93. /// </summary>
  94. public static long MemoryAvailable
  95. {
  96. get
  97. {
  98. try
  99. {
  100. using var mos = new ManagementClass("Win32_OperatingSystem");
  101. foreach (var mo in mos.GetInstances())
  102. {
  103. if (mo["FreePhysicalMemory"] != null)
  104. {
  105. return 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());
  106. }
  107. }
  108. return 0;
  109. }
  110. catch (Exception)
  111. {
  112. return 0;
  113. }
  114. }
  115. }
  116. #endregion
  117. #region 物理内存
  118. /// <summary>
  119. /// 获取物理内存
  120. /// </summary>
  121. public static long PhysicalMemory { get; }
  122. #endregion
  123. #region 查找所有应用程序标题
  124. /// <summary>
  125. /// 查找所有应用程序标题
  126. /// </summary>
  127. /// <param name="handle">应用程序标题范型</param>
  128. /// <returns>所有应用程序集合</returns>
  129. public static ArrayList FindAllApps(int handle)
  130. {
  131. var apps = new ArrayList();
  132. int hwCurr = GetWindow(handle, GwHwndfirst);
  133. while (hwCurr > 0)
  134. {
  135. int IsTask = WsVisible | WsBorder;
  136. int lngStyle = GetWindowLongA(hwCurr, GwlStyle);
  137. bool taskWindow = (lngStyle & IsTask) == IsTask;
  138. if (taskWindow)
  139. {
  140. int length = GetWindowTextLength(new IntPtr(hwCurr));
  141. StringBuilder sb = new StringBuilder(2 * length + 1);
  142. GetWindowText(hwCurr, sb, sb.Capacity);
  143. string strTitle = sb.ToString();
  144. if (!string.IsNullOrEmpty(strTitle))
  145. {
  146. apps.Add(strTitle);
  147. }
  148. }
  149. hwCurr = GetWindow(hwCurr, GwHwndnext);
  150. }
  151. return apps;
  152. }
  153. #endregion
  154. #region 获取CPU的数量
  155. /// <summary>
  156. /// 获取CPU的数量
  157. /// </summary>
  158. /// <returns>CPU的数量</returns>
  159. public static int GetCpuCount()
  160. {
  161. try
  162. {
  163. using var m = new ManagementClass("Win32_Processor");
  164. using var mn = m.GetInstances();
  165. return mn.Count;
  166. }
  167. catch (Exception)
  168. {
  169. return 0;
  170. }
  171. }
  172. #endregion
  173. #region 获取CPU信息
  174. private static List<ManagementBaseObject> _cpuObjects;
  175. /// <summary>
  176. /// 获取CPU信息
  177. /// </summary>
  178. /// <returns>CPU信息</returns>
  179. public static List<CpuInfo> GetCpuInfo()
  180. {
  181. try
  182. {
  183. using var mos = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  184. using var moc = mos.Get();
  185. _cpuObjects ??= moc.AsParallel().Cast<ManagementBaseObject>().ToList();
  186. return _cpuObjects.Select(mo => new CpuInfo
  187. {
  188. CpuLoad = CpuLoad,
  189. NumberOfLogicalProcessors = ProcessorCount,
  190. CurrentClockSpeed = mo.Properties["CurrentClockSpeed"].Value.ToString(),
  191. Manufacturer = mo.Properties["Manufacturer"].Value.ToString(),
  192. MaxClockSpeed = mo.Properties["MaxClockSpeed"].Value.ToString(),
  193. Type = mo.Properties["Name"].Value.ToString(),
  194. DataWidth = mo.Properties["DataWidth"].Value.ToString(),
  195. DeviceID = mo.Properties["DeviceID"].Value.ToString(),
  196. NumberOfCores = Convert.ToInt32(mo.Properties["NumberOfCores"].Value),
  197. Temperature = GetCPUTemperature()
  198. }).ToList();
  199. }
  200. catch (Exception)
  201. {
  202. return new List<CpuInfo>();
  203. }
  204. }
  205. #endregion
  206. #region 获取内存信息
  207. /// <summary>
  208. /// 获取内存信息
  209. /// </summary>
  210. /// <returns>内存信息</returns>
  211. public static RamInfo GetRamInfo()
  212. {
  213. var info = new RamInfo
  214. {
  215. MemoryAvailable = GetFreePhysicalMemory(),
  216. PhysicalMemory = GetTotalPhysicalMemory(),
  217. TotalPageFile = GetTotalVirtualMemory(),
  218. AvailablePageFile = GetTotalVirtualMemory() - GetUsedVirtualMemory(),
  219. AvailableVirtual = 1 - GetUsageVirtualMemory(),
  220. TotalVirtual = 1 - GetUsedPhysicalMemory()
  221. };
  222. return info;
  223. }
  224. #endregion
  225. #region 获取CPU温度
  226. /// <summary>
  227. /// 获取CPU温度
  228. /// </summary>
  229. /// <returns>CPU温度</returns>
  230. public static float GetCPUTemperature()
  231. {
  232. try
  233. {
  234. string str = "";
  235. using var mos = new ManagementObjectSearcher(@"root\WMI", "select * from MSAcpi_ThermalZoneTemperature");
  236. var moc = mos.Get();
  237. foreach (var mo in moc)
  238. {
  239. str += mo.Properties["CurrentTemperature"].Value.ToString();
  240. }
  241. //这就是CPU的温度了
  242. float temp = (float.Parse(str) - 2732) / 10;
  243. return (float)Math.Round(temp, 2);
  244. }
  245. catch (Exception)
  246. {
  247. return 0;
  248. }
  249. }
  250. #endregion
  251. #region WMI接口获取CPU使用率
  252. /// <summary>
  253. /// WMI接口获取CPU使用率
  254. /// </summary>
  255. /// <returns></returns>
  256. public static string GetProcessorData()
  257. {
  258. float d = GetCounterValue(CpuCounter, "Processor", "% Processor Time", "_Total");
  259. return CompactFormat ? (int)d + "%" : d.ToString("F") + "%";
  260. }
  261. #endregion
  262. #region 获取虚拟内存使用率详情
  263. /// <summary>
  264. /// 获取虚拟内存使用率详情
  265. /// </summary>
  266. /// <returns></returns>
  267. public static string GetMemoryVData()
  268. {
  269. float d = GetCounterValue(MemoryCounter, "Memory", "% Committed Bytes In Use", null);
  270. var str = d.ToString("F") + "% (";
  271. d = GetCounterValue(MemoryCounter, "Memory", "Committed Bytes", null);
  272. str += FormatBytes(d) + " / ";
  273. d = GetCounterValue(MemoryCounter, "Memory", "Commit Limit", null);
  274. return str + FormatBytes(d) + ") ";
  275. }
  276. /// <summary>
  277. /// 获取虚拟内存使用率
  278. /// </summary>
  279. /// <returns></returns>
  280. public static float GetUsageVirtualMemory()
  281. {
  282. return GetCounterValue(MemoryCounter, "Memory", "% Committed Bytes In Use", null);
  283. }
  284. /// <summary>
  285. /// 获取虚拟内存已用大小
  286. /// </summary>
  287. /// <returns></returns>
  288. public static float GetUsedVirtualMemory()
  289. {
  290. return GetCounterValue(MemoryCounter, "Memory", "Committed Bytes", null);
  291. }
  292. /// <summary>
  293. /// 获取虚拟内存总大小
  294. /// </summary>
  295. /// <returns></returns>
  296. public static float GetTotalVirtualMemory()
  297. {
  298. return GetCounterValue(MemoryCounter, "Memory", "Commit Limit", null);
  299. }
  300. #endregion
  301. #region 获取物理内存使用率详情
  302. /// <summary>
  303. /// 获取物理内存使用率详情描述
  304. /// </summary>
  305. /// <returns></returns>
  306. public static string GetMemoryPData()
  307. {
  308. string s = QueryComputerSystem("totalphysicalmemory");
  309. float totalphysicalmemory = Convert.ToSingle(s);
  310. float d = GetCounterValue(MemoryCounter, "Memory", "Available Bytes", null);
  311. d = totalphysicalmemory - d;
  312. s = CompactFormat ? "%" : "% (" + FormatBytes(d) + " / " + FormatBytes(totalphysicalmemory) + ")";
  313. d /= totalphysicalmemory;
  314. d *= 100;
  315. return CompactFormat ? (int)d + s : d.ToString("F") + s;
  316. }
  317. /// <summary>
  318. /// 获取物理内存总数,单位B
  319. /// </summary>
  320. /// <returns></returns>
  321. public static float GetTotalPhysicalMemory()
  322. {
  323. string s = QueryComputerSystem("totalphysicalmemory");
  324. return s.TryConvertTo<float>();
  325. }
  326. /// <summary>
  327. /// 获取空闲的物理内存数,单位B
  328. /// </summary>
  329. /// <returns></returns>
  330. public static float GetFreePhysicalMemory()
  331. {
  332. return GetCounterValue(MemoryCounter, "Memory", "Available Bytes", null);
  333. }
  334. /// <summary>
  335. /// 获取已经使用了的物理内存数,单位B
  336. /// </summary>
  337. /// <returns></returns>
  338. public static float GetUsedPhysicalMemory()
  339. {
  340. return GetTotalPhysicalMemory() - GetFreePhysicalMemory();
  341. }
  342. #endregion
  343. #region 获取硬盘的读写速率
  344. /// <summary>
  345. /// 获取硬盘的读写速率
  346. /// </summary>
  347. /// <param name="dd">读或写</param>
  348. /// <returns></returns>
  349. public static float GetDiskData(DiskData dd) => dd == DiskData.Read ? GetCounterValue(DiskReadCounter, "PhysicalDisk", "Disk Read Bytes/sec", "_Total") : dd == DiskData.Write ? GetCounterValue(DiskWriteCounter, "PhysicalDisk", "Disk Write Bytes/sec", "_Total") : dd == DiskData.ReadAndWrite ? GetCounterValue(DiskReadCounter, "PhysicalDisk", "Disk Read Bytes/sec", "_Total") + GetCounterValue(DiskWriteCounter, "PhysicalDisk", "Disk Write Bytes/sec", "_Total") : 0;
  350. #endregion
  351. #region 获取网络的传输速率
  352. /// <summary>
  353. /// 获取网络的传输速率
  354. /// </summary>
  355. /// <param name="nd">上传或下载</param>
  356. /// <returns></returns>
  357. public static float GetNetData(NetData nd)
  358. {
  359. if (InstanceNames.Length == 0)
  360. {
  361. return 0;
  362. }
  363. float d = 0;
  364. for (int i = 0; i < InstanceNames.Length; i++)
  365. {
  366. float receied = GetCounterValue(NetRecvCounters[i], "Network Interface", "Bytes Received/sec", InstanceNames[i]);
  367. float send = GetCounterValue(NetSentCounters[i], "Network Interface", "Bytes Sent/sec", InstanceNames[i]);
  368. switch (nd)
  369. {
  370. case NetData.Received:
  371. d += receied;
  372. break;
  373. case NetData.Sent:
  374. d += send;
  375. break;
  376. case NetData.ReceivedAndSent:
  377. d += receied + send;
  378. break;
  379. default:
  380. d += 0;
  381. break;
  382. }
  383. }
  384. return d;
  385. }
  386. #endregion
  387. /// <summary>
  388. /// 获取网卡硬件地址
  389. /// </summary>
  390. /// <returns></returns>
  391. public static IList<string> GetMacAddress()
  392. {
  393. //获取网卡硬件地址
  394. try
  395. {
  396. IList<string> list = new List<string>();
  397. using var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  398. using var moc = mc.GetInstances();
  399. foreach (var mo in moc)
  400. {
  401. if ((bool)mo["IPEnabled"])
  402. {
  403. list.Add(mo["MacAddress"].ToString());
  404. }
  405. }
  406. return list;
  407. }
  408. catch (Exception)
  409. {
  410. return new List<string>();
  411. }
  412. }
  413. /// <summary>
  414. /// 获取当前使用的IP
  415. /// </summary>
  416. /// <returns></returns>
  417. public static IPAddress GetLocalUsedIP()
  418. {
  419. return NetworkInterface.GetAllNetworkInterfaces().OrderByDescending(c => c.Speed).Where(c => c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up).SelectMany(n => n.GetIPProperties().UnicastAddresses.Select(u => u.Address)).FirstOrDefault();
  420. }
  421. /// <summary>
  422. /// 获取本机所有的ip地址
  423. /// </summary>
  424. /// <returns></returns>
  425. public static List<UnicastIPAddressInformation> GetLocalIPs()
  426. {
  427. var interfaces = NetworkInterface.GetAllNetworkInterfaces().OrderByDescending(c => c.Speed).Where(c => c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up); //所有网卡信息
  428. return interfaces.SelectMany(n => n.GetIPProperties().UnicastAddresses).ToList();
  429. }
  430. #region 将速度值格式化成字节单位
  431. /// <summary>
  432. /// 将速度值格式化成字节单位
  433. /// </summary>
  434. /// <param name="bytes"></param>
  435. /// <returns></returns>
  436. public static string FormatBytes(this double bytes)
  437. {
  438. int unit = 0;
  439. while (bytes > 1024)
  440. {
  441. bytes /= 1024;
  442. ++unit;
  443. }
  444. string s = CompactFormat ? ((int)bytes).ToString() : bytes.ToString("F") + " ";
  445. return s + (Unit)unit;
  446. }
  447. #endregion
  448. #region 查询计算机系统信息
  449. /// <summary>
  450. /// 获取计算机开机时间
  451. /// </summary>
  452. /// <returns>datetime</returns>
  453. public static DateTime BootTime()
  454. {
  455. var query = new SelectQuery("SELECT LastBootUpTime FROM Win32_OperatingSystem WHERE Primary='true'");
  456. using var searcher = new ManagementObjectSearcher(query);
  457. foreach (var mo in searcher.Get())
  458. {
  459. return ManagementDateTimeConverter.ToDateTime(mo.Properties["LastBootUpTime"].Value.ToString());
  460. }
  461. return DateTime.Now - TimeSpan.FromMilliseconds(Environment.TickCount & int.MaxValue);
  462. }
  463. /// <summary>
  464. /// 查询计算机系统信息
  465. /// </summary>
  466. /// <param name="type">类型名</param>
  467. /// <returns></returns>
  468. public static string QueryComputerSystem(string type)
  469. {
  470. try
  471. {
  472. string str = null;
  473. var mos = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
  474. foreach (var mo in mos.Get())
  475. {
  476. str = mo[type].ToString();
  477. }
  478. return str;
  479. }
  480. catch (Exception e)
  481. {
  482. return "未能获取到当前计算机系统信息,可能是当前程序无管理员权限,如果是web应用程序,请将应用程序池的高级设置中的进程模型下的标识设置为:LocalSystem;如果是普通桌面应用程序,请提升管理员权限后再操作。异常信息:" + e.Message;
  483. }
  484. }
  485. #endregion
  486. #region 获取环境变量
  487. /// <summary>
  488. /// 获取环境变量
  489. /// </summary>
  490. /// <param name="type">环境变量名</param>
  491. /// <returns></returns>
  492. public static string QueryEnvironment(string type) => Environment.ExpandEnvironmentVariables(type);
  493. #endregion
  494. #region 获取磁盘空间
  495. /// <summary>
  496. /// 获取磁盘可用空间
  497. /// </summary>
  498. /// <returns></returns>
  499. public static Dictionary<string, string> DiskFree()
  500. {
  501. try
  502. {
  503. var dic = new Dictionary<string, string>();
  504. var mos = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
  505. foreach (var mo in mos.Get())
  506. {
  507. if (null != mo["DeviceID"] && null != mo["FreeSpace"])
  508. {
  509. dic.Add(mo["DeviceID"].ToString(), FormatBytes(float.Parse(mo["FreeSpace"].ToString())));
  510. }
  511. }
  512. return dic;
  513. }
  514. catch (Exception)
  515. {
  516. return new Dictionary<string, string>()
  517. {
  518. { "null", "未能获取到当前计算机的磁盘信息,可能是当前程序无管理员权限,如果是web应用程序,请将应用程序池的高级设置中的进程模型下的标识设置为:LocalSystem;如果是普通桌面应用程序,请提升管理员权限后再操作。" }
  519. };
  520. }
  521. }
  522. /// <summary>
  523. /// 获取磁盘总空间
  524. /// </summary>
  525. /// <returns></returns>
  526. public static Dictionary<string, string> DiskTotalSpace()
  527. {
  528. try
  529. {
  530. var dic = new Dictionary<string, string>();
  531. using var mos = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
  532. foreach (var mo in mos.Get())
  533. {
  534. if (null != mo["DeviceID"] && null != mo["Size"])
  535. {
  536. dic.Add(mo["DeviceID"].ToString(), FormatBytes(float.Parse(mo["Size"].ToString())));
  537. }
  538. }
  539. return dic;
  540. }
  541. catch (Exception)
  542. {
  543. return new Dictionary<string, string>();
  544. }
  545. }
  546. /// <summary>
  547. /// 获取磁盘已用空间
  548. /// </summary>
  549. /// <returns></returns>
  550. public static Dictionary<string, string> DiskUsedSpace()
  551. {
  552. try
  553. {
  554. var dic = new Dictionary<string, string>();
  555. using var mos = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
  556. foreach (var mo in mos.Get())
  557. {
  558. if (null != mo["DeviceID"] && null != mo["Size"])
  559. {
  560. var free = mo["FreeSpace"];
  561. dic.Add(mo["DeviceID"].ToString(), FormatBytes(float.Parse(mo["Size"].ToString()) - free.ToString().ToDouble()));
  562. }
  563. }
  564. return dic;
  565. }
  566. catch (Exception)
  567. {
  568. return new Dictionary<string, string>()
  569. {
  570. { "null", "未能获取到当前计算机的磁盘信息,可能是当前程序无管理员权限,如果是web应用程序,请将应用程序池的高级设置中的进程模型下的标识设置为:LocalSystem;如果是普通桌面应用程序,请提升管理员权限后再操作。" }
  571. };
  572. }
  573. }
  574. /// <summary>
  575. /// 获取磁盘使用率
  576. /// </summary>
  577. /// <returns></returns>
  578. public static Dictionary<string, float> DiskUsage()
  579. {
  580. try
  581. {
  582. var dic = new Dictionary<string, float>();
  583. using var mos = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
  584. foreach (var mo in mos.Get())
  585. {
  586. var device = mo["DeviceID"];
  587. if (null != device)
  588. {
  589. var free = mo["FreeSpace"];
  590. var total = mo["Size"];
  591. if (null != total && total.ToString().TryConvertTo<float>() > 0)
  592. {
  593. dic.Add(device.ToString(), 1 - free.ToString().TryConvertTo<float>() / total.ToString().TryConvertTo<float>());
  594. }
  595. }
  596. }
  597. return dic;
  598. }
  599. catch (Exception)
  600. {
  601. return new Dictionary<string, float>()
  602. {
  603. { "未能获取到当前计算机的磁盘信息,可能是当前程序无管理员权限,如果是web应用程序,请将应用程序池的高级设置中的进程模型下的标识设置为:LocalSystem;如果是普通桌面应用程序,请提升管理员权限后再操作。", 0 }
  604. };
  605. }
  606. }
  607. #endregion
  608. private static float GetCounterValue(PerformanceCounter pc, string categoryName, string counterName, string instanceName)
  609. {
  610. pc.CategoryName = categoryName;
  611. pc.CounterName = counterName;
  612. pc.InstanceName = instanceName;
  613. return pc.NextValue();
  614. }
  615. #region Win32API声明
  616. #pragma warning disable 1591
  617. [DllImport("kernel32")]
  618. public static extern void GetWindowsDirectory(StringBuilder winDir, int count);
  619. [DllImport("kernel32")]
  620. public static extern void GetSystemDirectory(StringBuilder sysDir, int count);
  621. [DllImport("kernel32")]
  622. public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
  623. [DllImport("kernel32")]
  624. public static extern void GlobalMemoryStatus(ref MemoryInfo meminfo);
  625. [DllImport("kernel32")]
  626. public static extern void GetSystemTime(ref SystemtimeInfo stinfo);
  627. [DllImport("IpHlpApi.dll")]
  628. public static extern uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder);
  629. [DllImport("User32")]
  630. public static extern int GetWindow(int hWnd, int wCmd);
  631. [DllImport("User32")]
  632. public static extern int GetWindowLongA(int hWnd, int wIndx);
  633. [DllImport("user32.dll")]
  634. public static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
  635. [DllImport("user32", CharSet = CharSet.Auto)]
  636. public static extern int GetWindowTextLength(IntPtr hWnd);
  637. #pragma warning restore 1591
  638. #endregion
  639. }
  640. }