使用VMI获取服务器端物理内存、CPU

本文介绍了一种通过WMI远程连接的方式查询目标计算机的CPU使用率及总物理内存的方法。利用C#语言实现了创建连接、执行查询并展示结果的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 ConnectionOptions options =
           new ConnectionOptions();
            options.Username = "administrator";
            options.Password = "********";
 
 
            // Make a connection to a remote computer.
            // Replace the "FullComputerName" section of the
            // string "\\\\FullComputerName\\root\\cimv2" with
            // the full computer name or IP address of the
            // remote computer.
            ManagementScope scope =
                new ManagementScope(
                "\\\\FullComputerName\\root\\cimv2", options);
            scope.Connect();
 
            //Query system for Operating System information
            ObjectQuery query = new ObjectQuery(
                "select * from Win32_Processor");
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher(scope, query);
 
            ManagementObjectCollection queryCollection = searcher.Get();
            foreach (ManagementObject m in queryCollection)
            {
                Console.WriteLine("CPU : {0}",
                    m["LoadPercentage"].ToString());   //cpu 使用率
            }
 
            double totalMem = 0;
            string strMsg = "";
            ObjectQuery query1 = new ObjectQuery(
               "Select TotalPhysicalMemory from Win32_LogicalMemoryConfiguration");
            ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query1);
            int i = 0;
            ManagementObjectCollection queryCollection1 = search.Get();
            foreach (ManagementObject info in queryCollection1)
            {
                i = i + 1;
                totalMem += Convert.ToDouble(info["TotalPhysicalMemory"].ToString()) / 1024;
                strMsg += string.Format("物理内存({0}):大小:{1}MB", i, Convert.ToDouble(info["TotalPhysicalMemory"].ToString()) / 1024);
            }
            strMsg += string.Format("总物理内存的大小:{0}MB <br/>", totalMem);
            Console.WriteLine(strMsg);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值