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

c#查看电脑内存

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Management;
using System.Runtime.Serialization.Formatters.Binary;

using RemoteDeploy.Core;
using RemoteDeploy.Core.Attributes;
using RemoteDeploy.CommandHelper;

using Common.Logging;

namespace RemoteDeploy.Commands
{
    [Command(Name = "memoryMonitorCommand", Description = "monitoring memory usage")]
    public class MemoryMonitorCommand
    {
        private const string PROPERTY_CAPACITY = "Capacity";
        private const string PROPERTY_AVAILABLE_BYTES = "AvailableBytes";
        private const string PROPERTY_PROCESS_ID = "ProcessId";
        private const string PROPERTY_PROCESS_NAME = "Name";
        private const string PROPERTY_WORKING_SET_SIZE = "WorkingSetSize";

        private const string QUERY_MEMORY1 = "SELECT * FROM Win32_PhysicalMemory";
        private const string QUERY_MEMORY2 = "SELECT * FROM Win32_PerfRawData_PerfOS_Memory";
        private const string QUERY_MEMORY3 = "SELECT * FROM Win32_Process";

        ILog logger = LogManager.GetLogger(typeof(MemoryMonitorCommand));

        [Parameter(Name = "appNames")]
        public string AppNames;

        [Execute]
        public void Run()
        {
            var query1 = new SelectQuery(QUERY_MEMORY1);
            var query2 = new SelectQuery(QUERY_MEMORY2);
            var query3 = new SelectQuery(QUERY_MEMORY3);
            var searcher1 = new ManagementObjectSearcher(query1);
            var searcher2 = new ManagementObjectSearcher(query2);
            var searcher3 = new ManagementObjectSearcher(query3);

            var capacity = 0.0;
            var free = 0.0;
            foreach (var o in searcher1.Get())
                capacity += (double)Convert.ToUInt64(o[PROPERTY_CAPACITY]) / (double)(1024 * 1024);
            foreach (var o in searcher2.Get())
                free += (double)Convert.ToUInt64(o[PROPERTY_AVAILABLE_BYTES]) / (double)(1024 * 1024);
            //var appsUsed = new Dictionary<string, double>();
            //var apps = AppNames.Split(new[] { \';\' }, StringSplitOptions.RemoveEmptyEntries);
            //foreach (var app in apps)
            //{
            //    var appUsed = 0.0;
            //    foreach (var o in searcher3.Get())
            //        if (string.Equals(Convert.ToString(o[PROPERTY_PROCESS_NAME]), app, StringComparison.OrdinalIgnoreCase))
            //            appUsed += (double)Convert.ToUInt64(o[PROPERTY_WORKING_SET_SIZE]) / (double)(1024 * 1024);
            //    appsUsed.Add(app, appUsed);
            //}
            var appsUsed = new Dictionary<string, Dictionary<uint, double>>();
            var apps = AppNames.Split(new[] { \';\' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var o in searcher3.Get())
            {
                var appName = Convert.ToString(o[PROPERTY_PROCESS_NAME]);
                foreach (var app in apps)
                {
                    if (string.Equals(appName, app, StringComparison.OrdinalIgnoreCase))
                    {
                        var appId = Convert.ToUInt32(o[PROPERTY_PROCESS_ID]);
                        var workingSetSize = (double)Convert.ToUInt64(o[PROPERTY_WORKING_SET_SIZE]) / (double)(1024 * 1024);
                        if (appsUsed.ContainsKey(appName))
                            appsUsed[appName].Add(appId, workingSetSize);
                        else
                            appsUsed.Add(appName, new Dictionary<uint, double>() 
                        {
                            { 
                                appId, workingSetSize 
                            } 
                        });
                        break;
                    }
                }
            }

            var memInfo = new MemoryInfo()
            {
                Capacity = capacity,
                Free = free,
                Used = capacity - free,
                AppsUsed = appsUsed
            };

            var bin = new BinaryFormatter();
            var ms = new MemoryStream();
            bin.Serialize(ms, memInfo);
            logger.Info("memory monitor begin execute callback");
            CommandContext.Instance.SendMessage("memory usage info", ms.ToArray());
            logger.Info("memory monitor end execute callback");
        }

    }
}

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
C#仿QQ皮肤-主窗体MainForm和Main的实现[转]发布时间:2022-07-14
下一篇:
C#获取系统信息发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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