posts - 710,  comments - 81,  views - 260万
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

 

复制代码
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 using System.Management;
  7 using System.Diagnostics;
  8 using Newtonsoft.Json;
  9 using Newtonsoft.Json.Linq;
 10 
 11 namespace GH.IMS
 12 {
 13     public class SystemMonit
 14     {
 15         private ManagementScope Ms;
 16         
 17         public SystemMonit()
 18         {
 19             Ms = new ManagementScope();
 20         }
 21 
 22         public SystemMonit(String ip, String userName, String password)
 23         {
 24             ConnectionOptions Conn = new ConnectionOptions();
 25             Conn.Username = userName;
 26             Conn.Password = password;
 27             Ms = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", ip), Conn);
 28 
 29             Ms.Connect();
 30         }
 31 
 32 
 33         public JArray GetLogicalDisk()
 34         {
 35             return GetInfoFromWMI("Win32_LogicalDisk", "DeviceID,Size,FreeSpace");
 36         }
 37 
 38         public  JArray GetNAConfigurationInfo()
 39         {
 40             return GetInfoFromWMI("Win32_NetworkAdapterConfiguration",
 41                 "IPAddress,DefaultIPGateway,DNSServerSearchOrder,IPSubnet,MACAddress");
 42         }
 43 
 44 
 45         public JArray GetProcessorInfo()
 46         {
 47             return GetInfoFromWMI("Win32_Processor",
 48                 "Name,Family,L2CacheSize,Manufacturer,MaxClockSpeed,ProcessorId,LoadPercentage");
 49         }
 50 
 51         public JArray GetPhysicalMemoryInfo()
 52         {
 53             JArray jArrayAvailable = GetInfoFromWMI("Win32_PerfRawData_PerfOS_Memory",
 54                 "AvailableMBytes");
 55             JArray jArrayTotal = GetInfoFromWMI("Win32_LogicalMemoryConfiguration",
 56                 "TotalPhysicalMemory");
 57             JArray result=new JArray();
 58             result.Add(jArrayAvailable);
 59             result.Add(jArrayTotal);
 60             return result;
 61         } 
 62         /// <summary>
 63         /// 通过wmi查找相关信息
 64         /// </summary>
 65         /// <param name="name">系统配置的名称</param>
 66         /// <param name="fields">需要查询的字段,多个字段用","分隔</param>
 67         /// <returns></returns>
 68         public JArray GetInfoFromWMI(String name, String fields)
 69         {
 70             JArray jarray = new JArray();
 71             try
 72             {
 73                 ManagementObjectCollection mocObj =
 74                     new ManagementObjectSearcher(Ms, 
 75                         new ObjectQuery(       
 76                             String.Format("Select {0} From {1}", fields, name))).
 77                     Get();
 78                 JObject jObj;
 79                 foreach (ManagementObject mo in mocObj)
 80                 {
 81                     jObj = new JObject();
 82                     foreach (String fieldName in fields.Split(','))
 83                     {
 84                         if (mo[fieldName] != null)
 85                             jObj.Add(new JProperty(fieldName, mo[fieldName]));
 86                     }
 87                     jarray.Add(jObj);
 88                 }
 89             }
 90             catch
 91             {
 92                 jarray = null;
 93             }
 94             return jarray;
 95         }
 96 
 97         private enum Unit { B, KB, MB, GB, ER }
 98         private string FormatBytes(double bytes)
 99         {
100             int unit = 0;
101             while (bytes > 1024)
102             {
103                 bytes /= 1024;
104                 ++unit;
105             }
106             return string.Format("{0} {1}", 
107                 bytes.ToString("F"), 
108                 ((Unit)unit).ToString());
109         }
110     }
111 }
复制代码

 程序员的基础教程:菜鸟程序员

posted on   itprobie-菜鸟程序员  阅读(536)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示