• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[No0000112]ComputerInfo,C#获取计算机信息(cpu使用率,内存占用率,硬盘,网络信息 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

github地址:https://github.com/charygao/SmsComputerMonitor

软件用于实时监控当前系统资源等情况,并调用接口,当资源被超额占用时,发送警报到个人手机;界面模拟Console的显示方式,信息缓冲大小由配置决定;可以定制监控的资源,包括:

  • cpu使用率;
  • 内存使用率;
  • 磁盘使用率;
  • 网络使用率;
  • 进程线程数;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace SmsComputerMonitor
{
    public class ComputerInfo
    {
        #region Fields and Properties

        private readonly ManagementScope _LocalManagementScope;

        private readonly PerformanceCounter _pcCpuLoad; //CPU计数器,全局
        private const int GwHwndfirst = 0;
        private const int GwHwndnext = 2;
        private const int GwlStyle = -16;
        private const int WsBorder = 8388608;
        private const int WsVisible = 268435456;

        #region CPU占用率

        /// <summary>
        ///     获取CPU占用率(系统CPU使用率)
        /// </summary>
        public float CpuLoad => _pcCpuLoad.NextValue();

        #endregion

        #region 本地主机名

        public string HostName => Dns.GetHostName();

        #endregion

        #region 本地IPV4列表

        public List<IPAddress> IpAddressV4S
        {
            get
            {
                var ipV4S = new List<IPAddress>();
                foreach (var address in Dns.GetHostAddresses(Dns.GetHostName()))
                    if (address.AddressFamily == AddressFamily.InterNetwork)
                        ipV4S.Add(address);
                return ipV4S;
            }
        }

        #endregion

        #region 本地IPV6列表

        public List<IPAddress> IpAddressV6S
        {
            get
            {
                var ipV6S = new List<IPAddress>();
                foreach (var address in Dns.GetHostAddresses(Dns.GetHostName()))
                    if (address.AddressFamily == AddressFamily.InterNetworkV6)
                        ipV6S.Add(address);
                return ipV6S;
            }
        }

        #endregion

        #region 可用内存

        /// <summary>
        ///     获取可用内存
        /// </summary>
        public long MemoryAvailable
        {
            get
            {
                long availablebytes = 0;
                var managementClassOs = new ManagementClass("Win32_OperatingSystem");
                foreach (var managementBaseObject in managementClassOs.GetInstances())
                    if (managementBaseObject["FreePhysicalMemory"] != null)
                        availablebytes = 1024 * long.Parse(managementBaseObject["FreePhysicalMemory"].ToString());
                return availablebytes;
            }
        }

        #endregion

        #region 物理内存

        /// <summary>
        ///     获取物理内存
        /// </summary>
        public long PhysicalMemory { get; }

        #endregion

        #region CPU个数

        /// <summary>
        ///     获取CPU个数
        /// </summary>
        // ReSharper disable once UnusedAutoPropertyAccessor.Global
        public int ProcessorCount { get; }

        #endregion

        #region 已用内存大小

        public long SystemMemoryUsed => PhysicalMemory - MemoryAvailable;

        #endregion

        #endregion

        #region  Constructors

        /// <summary>
        ///     构造函数,初始化计数器等
        /// </summary>
        public ComputerInfo()
        {
            _LocalManagementScope = new ManagementScope($"\\\\{HostName}\\root\\cimv2");
            //初始化CPU计数器
            _pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total") {MachineName = "."};
            _pcCpuLoad.NextValue();

            //CPU个数
            ProcessorCount = Environment.ProcessorCount;

            //获得物理内存
            var managementClass = new ManagementClass("Win32_ComputerSystem");
            var managementObjectCollection = managementClass.GetInstances();
            foreach (var managementBaseObject in managementObjectCollection)
                if (managementBaseObject["TotalPhysicalMemory"] != null)
                    PhysicalMemory = long.Parse(managementBaseObject["TotalPhysicalMemory"].ToString());
        }

        #endregion

        #region  Methods

        #region 结束指定进程

        /// <summary>
        ///     结束指定进程
        /// </summary>
        /// <param name="pid">进程的 Process ID</param>
        public static void EndProcess(int pid)
        {
            try
            {
                var process = Process.GetProcessById(pid);
                process.Kill();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        #endregion

        #region 查找所有应用程序标题

        /// <summary>
        ///     查找所有应用程序标题
        /// </summary>
        /// <returns>应用程序标题范型</returns>
        public static List<string> FindAllApps(int handle)
        {
            var apps = new List<string>();

            var hwCurr = GetWindow(handle, GwHwndfirst);

            while (hwCurr > 0)
            {
                var IsTask = WsVisible | WsBorder;
                var lngStyle = GetWindowLongA(hwCurr, GwlStyle);
                var taskWindow = (lngStyle & IsTask) == IsTask;
                if (taskWindow)
                {
                    var length = GetWindowTextLength(new IntPtr(hwCurr));
                    var sb = new StringBuilder(2 * length + 1);
                    GetWindowText(hwCurr, sb, sb.Capacity);
                    var strTitle = sb.ToString();
                    if (!string.IsNullOrEmpty(strTitle))
                        apps.Add(strTitle);
                }
                hwCurr = GetWindow(hwCurr, GwHwndnext);
            }

            return apps;
        }

        #endregion

        public static List<PerformanceCounterCategory> GetAllCategories(bool isPrintRoot = true,
            bool isPrintTree = true)
        {
            var result = new List<PerformanceCounterCategory>();
            foreach (var category in PerformanceCounterCategory.GetCategories())
            {
                result.Add(category);
                if (isPrintRoot)
                {
                    PerformanceCounter[] categoryCounters;
                    switch (category.CategoryType)
                    {
                        case PerformanceCounterCategoryType.SingleInstance:
                            categoryCounters = category.GetCounters();
                            PrintCategoryAndCounters(category, categoryCounters, isPrintTree);
                            break;
                        case PerformanceCounterCategoryType.MultiInstance:
                            var categoryCounterInstanceNames = category.GetInstanceNames();
                            if (categoryCounterInstanceNames.Length > 0)
                            {
                                categoryCounters = category.GetCounters(categoryCounterInstanceNames[0]);
                                PrintCategoryAndCounters(category, categoryCounters, isPrintTree);
                            }

                            break;
                        case PerformanceCounterCategoryType.Unknown:
                            categoryCounters = category.GetCounters();
                            PrintCategoryAndCounters(category, categoryCounters, isPrintTree);
                            break;
                        //default: break;
                    }
                }
            }
            return result;
        }

        /// <summary>
        ///     获取本地所有磁盘
        /// </summary>
        /// <returns></returns>
        public static DriveInfo[] GetAllLocalDriveInfo()
        {
            return DriveInfo.GetDrives();
        }

        /// <summary>
        ///     获取本机所有进程
        /// </summary>
        /// <returns></returns>
        public static Process[] GetAllProcesses()
        {
            return Process.GetProcesses();
        }

        public static List<PerformanceCounter> GetAppointedCategorieCounters(
            PerformanceCategoryEnums.PerformanceCategoryEnum categorieEnum, bool isPrintRoot = true,
            bool isPrintTree = true)
        {
            var result = new List<PerformanceCounter>();
            var categorieName = PerformanceCategoryEnums.GetCategoryNameString(categorieEnum);
            if (PerformanceCounterCategory.Exists(categorieName))
            {
                var category = new PerformanceCounterCategory(categorieName);
                PerformanceCounter[] categoryCounters;
                switch (category.CategoryType)
                {
                    case PerformanceCounterCategoryType.Unknown:
                        categoryCounters = category.GetCounters();
                        result = categoryCounters.ToList();
                        if (isPrintRoot)
                            PrintCategoryAndCounters(category, categoryCounters, isPrintTree);
                        break;
                    case PerformanceCounterCategoryType.SingleInstance:
                        categoryCounters = category.GetCounters();
                        result = categoryCounters.ToList();
                        if (isPrintRoot)
                            PrintCategoryAndCounters(category, categoryCounters, isPrintTree);
                        break;
                    case PerformanceCounterCategoryType.MultiInstance:
                        var categoryCounterInstanceNames = category.GetInstanceNames();
                        if (categoryCounterInstanceNames.Length > 0)
                        {
                            categoryCounters = category.GetCounters(categoryCounterInstanceNames[0]);
                            result = categoryCounters.ToList();
                            if (isPrintRoot)
                                PrintCategoryAndCounters(category, categoryCounters, isPrintTree);
                        }
                        break;
                    //default: break;
                }
            }
            return result;
        }

        /// <summary>
        ///     获取指定磁盘可用大小
        /// </summary>
        /// <param name="drive"></param>
        /// <returns></returns>
        public static long GetDriveAvailableFreeSpace(DriveInfo drive)
        {
            return drive.AvailableFreeSpace;
        }

        /// <summary>
        ///     获取指定磁盘总空白大小
        /// </summary>
        /// <param name="drive"></param>
        /// <returns></returns>
        public static long GetDriveTotalFreeSpace(DriveInfo drive)
        {
            return drive.TotalFreeSpace;
        }

        /// <summary>
        ///     获取指定磁盘总大小
        /// </summary>
        /// <param name="drive"></param>
        /// <returns></returns>
        public static long GetDriveTotalSize(DriveInfo drive)
        {
            return drive.TotalSize;
        }

        #region 获取当前使用的IP,超时时间至少1s 

        /// <summary>
        ///     获取当前使用的IP,超时时间至少1s
        /// </summary>
        /// <returns></returns>
        public static string GetLocalIP(TimeSpan timeOut, bool isBelieveTimeOutValue = false, bool recordLog = true)
        {
            if (timeOut < new TimeSpan(0, 0, 1))
                timeOut = new TimeSpan(0, 0, 1);
            var isTimeOut = RunApp("route", "print", timeOut, out var result, recordLog);
            if (isTimeOut && !isBelieveTimeOutValue)
                try
                {
                    var tcpClient = new TcpClient();
                    tcpClient.Connect("www.baidu.com", 80);
                    var ip = ((IPEndPoint) tcpClient.Client.LocalEndPoint).Address.ToString();
                    tcpClient.Close();
                    return ip;
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    return null;
                }
            var getMatchedGroup = Regex.Match(result, @"0.0.0.0\s+0.0.0.0\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)");
            if (getMatchedGroup.Success)
                return getMatchedGroup.Groups[2].Value;
            return null;
        }

        #endregion

        #region 获取本机主DNS

        /// <summary>
        ///     获取本机主DNS
        /// </summary>
        /// <returns></returns>
        public static string GetPrimaryDNS(bool recordLog = true)
        {
            RunApp("nslookup", "", new TimeSpan(0, 0, 1), out var result, recordLog, true); //nslookup会超时
            var getMatchedGroup = Regex.Match(result, @"\d+\.\d+\.\d+\.\d+");
            if (getMatchedGroup.Success)
                return getMatchedGroup.Value;
            return null;
        }

        #endregion

        /// <summary>
        ///     获取指定进程最大线程数
        /// </summary>
        /// <returns></returns>
        public static int GetProcessMaxThreadCount(Process process)
        {
            return process.Threads.Count;
        }

        /// <summary>
        ///     获取指定进程最大线程数
        /// </summary>
        /// <returns></returns>
        public static int GetProcessMaxThreadCount(string processName)
        {
            var maxThreadCount = -1;
            foreach (var process in Process.GetProcessesByName(processName))
                if (maxThreadCount < process.Threads.Count)
                    maxThreadCount = process.Threads.Count;
            return maxThreadCount;
        }

        private static void PrintCategoryAndCounters(PerformanceCounterCategory category,
            PerformanceCounter[] categoryCounters, bool isPrintTree)
        {
            Console.WriteLine($@"===============>{category.CategoryName}:[{categoryCounters.Length}]");
            if (isPrintTree)
                foreach (var counter in categoryCounters)
                    Console.WriteLine($@"   ""{category.CategoryName}"", ""{counter.CounterName}""");
        }

        /// <summary>
        ///     运行一个控制台程序并返回其输出参数。耗时至少1s
        /// </summary>
        /// <param name="filename">程序名</param>
        /// <param name="arguments">输入参数</param>
        /// <param name="result"></param>
        /// <param name="recordLog"></param>
        /// <param name="timeOutTimeSpan"></param>
        /// <param name="needKill"></param>
        /// <returns></returns>
        private static bool RunApp(string filename, string arguments, TimeSpan timeOutTimeSpan, out string result,
            bool recordLog = true, bool needKill = false)
        {
            try
            {
                var stopwatch = Stopwatch.StartNew();
                if (recordLog)
                    Console.WriteLine($@"{filename} {arguments}");
                if (timeOutTimeSpan < new TimeSpan(0, 0, 1))
                    timeOutTimeSpan = new TimeSpan(0, 0, 1);
                var isTimeOut = false;
                var process = new Process
                {
                    StartInfo =
                    {
                        FileName = filename,
                        CreateNoWindow = true,
                        Arguments = arguments,
                        RedirectStandardOutput = true,
                        RedirectStandardInput = true,
                        UseShellExecute = false
                    }
                };
                process.Start();
                using (var streamReader = new StreamReader(process.StandardOutput.BaseStream, Encoding.Default))
                {
                    if (needKill)
                    {
                        Thread.Sleep(200);
                        process.Kill();
                    }
                    while (!process.HasExited)
                        if (stopwatch.Elapsed > timeOutTimeSpan)
                        {
                            process.Kill();
                            stopwatch.Stop();
                            isTimeOut = true;
                            break;
                        }
                    result = streamReader.ReadToEnd();
                    streamReader.Close();
                    if (recordLog)
                        Console.WriteLine($@"返回[{result}]耗时:[{stopwatch.Elapsed}]是否超时:{isTimeOut}");
                    return isTimeOut;
                }
            }
            catch (Exception exception)
            {
                result = exception.ToString();
                Console.WriteLine($@"出错[{result}]");
                return true;
            }
        }

        #endregion

        #region 获取本地/远程所有内存信息

        /// <summary>
        ///     需要启动RPC服务,获取本地内存信息:
        /// </summary>
        /// <returns></returns>
        public List<Dictionary<string, string>> GetWin32PhysicalMemoryInfos(
            PhysicalMemoryInfoEnums.PhysicalMemoryInfoEnum eEnum = PhysicalMemoryInfoEnums.PhysicalMemoryInfoEnum.All,
            bool isPrint = true)
        {
            var result = new List<Dictionary<string, string>>();
            try
            {
                _LocalManagementScope.Connect();
                foreach (var managementBaseObject in new ManagementObjectSearcher(_LocalManagementScope,
                        new SelectQuery(
                            $"SELECT {PhysicalMemoryInfoEnums.GetQueryString(eEnum)} FROM Win32_PhysicalMemory"))
                    .Get())
                {
                    if (isPrint)
                        Console.WriteLine($@"<=========={managementBaseObject}===========>");
                    var thisObjectsDictionary = new Dictionary<string, string>();
                    foreach (var property in managementBaseObject.Properties)
                    {
                        if (isPrint)
                            Console.WriteLine(
                                $@"[{property.Name,40}] -> [{property.Value,-40}]//{
                                        PhysicalMemoryInfoEnums.GetDescriptionString(property.Name)
                                    }.");
                        thisObjectsDictionary.Add(property.Name, property.Value.ToString());
                    }
                    result.Add(thisObjectsDictionary);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
            return result;
        }

        /// <summary>
        ///     需要启动RPC服务,获取远程内存信息:
        /// </summary>
        /// <returns></returns>
        public List<Dictionary<string, string>> GetWin32PhysicalMemoryInfos(
            string remoteHostName,
            PhysicalMemoryInfoEnums.PhysicalMemoryInfoEnum eEnum = PhysicalMemoryInfoEnums.PhysicalMemoryInfoEnum.All,
            bool isPrint = true)
        {
            var result = new List<Dictionary<string, string>>();
            try
            {
                var managementScope = new ManagementScope($"\\\\{remoteHostName}\\root\\cimv2");
                managementScope.Connect();
                foreach (var managementBaseObject in new ManagementObjectSearcher(managementScope,
                        new SelectQuery(
                            $"SELECT {PhysicalMemoryInfoEnums.GetQueryString(eEnum)} FROM Win32_PhysicalMemory"))
                    .Get())
                {
                    if (isPrint)
                        Console.WriteLine($@"<=========={managementBaseObject}===========>");
                    var thisObjectsDictionary = new Dictionary<string, string>();
                    foreach (var property in managementBaseObject.Properties)
                    {
                        if (isPrint)
                            Console.WriteLine(
                                $@"[{property.Name,40}] -> [{property.Value,-40}]//{
                                        PhysicalMemoryInfoEnums.GetDescriptionString(property.Name)
                                    }.");
                        thisObjectsDictionary.Add(property.Name, property.Value.ToString());
                    }
                    result.Add(thisObjectsDictionary);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
            return result;
        }

        /// <summary>
        ///     需要启动RPC服务,获取远程内存信息:
        /// </summary>
        /// <returns></returns>
        public List<Dictionary<string, string>> GetWin32PhysicalMemor 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ MySQL编程发布时间:2022-07-13
下一篇:
C# 字符串的连接发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap