C#检查、核对Memory Information

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace MemoryTs
{
    internal enum WmiType
    {
        Win32_Processor,
        Win32_PerfFormattedData_PerfOS_Memory,
        Win32_PhysicalMemory,
        Win32_NetworkAdapterConfiguration,
        Win32_LogicalDisk
    }

    internal class Program
    {
        private static List<MemoryInfoEntity> memoryInfo;//内存信息
        private static Dictionary<string, ManagementObjectCollection> WmiDict = new Dictionary<string, ManagementObjectCollection>();
        private static MemoryInfoEntity tempMemoryInfo;
        static int Main(string[] args)
        {
            Console.Clear();//清空
            Initial();
            if (GetMemoryNumber())
            {
                int i = 0;
                tempMemoryInfo = new MemoryInfoEntity();
                memoryInfo.ForEach((m) => {
                    i++;
                    tempMemoryInfo.PortNum = tempMemoryInfo.PortNum+(i < memoryInfo.Count ? m.PortNum + "-" : m.PortNum);
                    tempMemoryInfo.Capacity= tempMemoryInfo.Capacity+(i < memoryInfo.Count ? m.Capacity + "-" : m.Capacity);
                    tempMemoryInfo.TradeName= tempMemoryInfo.TradeName+(i < memoryInfo.Count ? m.TradeName + "-" : m.TradeName);
                });

                if (args.Length == 1 && args[0].ToLower()==@"/s")
                {
                    Console.WriteLine($@"MemoryTs.exe /c {tempMemoryInfo.PortNum} {tempMemoryInfo.Capacity} {tempMemoryInfo.TradeName}");
                    Console.ReadKey();
                    return 1;
                }
                else if (args.Length == 2 && args[0].ToLower()==@"/c")//核对端口测试
                {
                    if (tempMemoryInfo.PortNum.ToLower() == args[1].ToLower().Trim())
                    {
                        ShowInfo("Memory Port Num Check Test Pass..",1);
                        return 0;
                    }
                    else
                    {
                        ShowInfo("Memory Port Num Check Test Fail..", 2);
                        return 1;
                    }
                }
                else if (args.Length == 3 && args[0].ToLower()==@"/c")//核对端口和容量
                {
                    if (tempMemoryInfo.PortNum.ToLower() == args[1].ToLower().Trim() &&
                        tempMemoryInfo.Capacity.ToLower() == args[2].ToLower().Trim())
                    {
                        ShowInfo("Memory Port_Num and Capacity Check Test Pass..", 1);
                        return 0;
                    }
                    else
                    {
                        ShowInfo("Memory Port_Num and Capacity Check Test Fail..", 2);
                        return 1;
                    }
                }
                else if (args.Length == 4 && args[0].ToLower() == @"/c")
                {
                    if (tempMemoryInfo.PortNum.ToLower() == args[1].ToLower().Trim() &&
                        tempMemoryInfo.Capacity.ToLower() == args[2].ToLower().Trim() &&
                        tempMemoryInfo.TradeName.ToLower() == args[3].ToLower().Trim())
                    {
                        ShowInfo("Memory Port_Num && Capacity && TradeName Check Test Pass..", 1);
                        return 0;
                    }
                    else
                    {
                        ShowInfo("Memory Port_Num && Capacity && TradeName Check Test Fail..", 2);
                        return 1;
                    }
                }
                Help();
                return 1;
            }
            else
            {
                ShowInfo("Read Memory Info Fail!!",0);
                return 1;
            }
        }

        /// <summary>
        /// 帮助信息
        /// </summary>
        private static void Help()
        {
            Console.WriteLine($@"          Memory socket&capacity show / check  Utility - Build V1.0.0.0");
            Console.WriteLine($@"<C>Copyright Shen Bo. Editor shenbo200809@126.com . All Right Reserved.");
            Console.WriteLine($@"--------------------------------------------------------------------------------Usage: MemoryTs.exe");
            Console.WriteLine($@"      /s    Show all Memory socket and Information");
            Console.WriteLine($@"      /c    Check Specify socket Information.");
            Console.WriteLine($@"--------------------------------------------------------------------------------Example:WmiMemory.exe /Show");
            Console.WriteLine($@"      MemoryTs.exe /s");
            Console.WriteLine($@"      MemoryTs.exe /c 0-1-2");
            Console.WriteLine($@"      MemoryTs.exe /c 0-1-2 4096-4096-4096");
            Console.WriteLine($@"      MemoryTs.exe /c 0-1-2 4096-4096-4096 KHX2400C15-KHX2400C15-KHX2400C15");
            Console.ReadKey();
        }

        /// <summary>
        /// 显示的信息
        /// </summary>
        /// <param name="text"></param>
        /// <param name="state"></param>
        private static void ShowInfo(string text, int state)
        {
            Console.ForegroundColor = state == 1 ? ConsoleColor.Green : ConsoleColor.Red;
            Console.WriteLine(text);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private static void Initial()
        {
            var names = Enum.GetNames(typeof(WmiType));
            foreach (string name in names)
            {
                WmiDict.Add(name, new ManagementObjectSearcher("SELECT * FROM " + name).Get());
            }
        }


        /// <summary>
        /// 获取内存编号
        /// </summary>
        /// <returns></returns>
        public static bool GetMemoryNumber()
        {
            var query = WmiDict[WmiType.Win32_PhysicalMemory.ToString()];
            //var collection = query.Get();
            memoryInfo = new List<MemoryInfoEntity>();
            foreach (var obj in query)
            {
                string[]arrystr = (obj["PartNumber"].ToString()).Split(new string[] { @"/"},StringSplitOptions.RemoveEmptyEntries);
                memoryInfo.Add(new MemoryInfoEntity() { 
                    PortNum=(memoryInfo.Count+1).ToString().Trim(),
                    TradeName = arrystr[0].ToString().Trim(),
                    Capacity = arrystr[1].ToString().Trim()
                });
            }
            return memoryInfo.Count > 0 ? true : false;
        }

        /// <summary>
        /// 获取内存信息
        /// </summary>
        /// <returns></returns>
        public static string MemoryInfo()
        {
            StringBuilder sr = new StringBuilder();
            long capacity = 0;
            var query = WmiDict[WmiType.Win32_PhysicalMemory.ToString()];
            int index = 1;
            foreach (var obj in query)
            {
                sr.Append("内存" + index + "频率:" + obj["ConfiguredClockSpeed"] + ";");
                capacity += Convert.ToInt64(obj["Capacity"]);
                index++;
            }
            sr.Append("总物理内存:");
            sr.Append(capacity / 1024 / 1024 + "MB;");

            query = WmiDict[WmiType.Win32_PerfFormattedData_PerfOS_Memory.ToString()];
            sr.Append("总可用内存:");
            long available = 0;
            foreach (var obj in query)
            {
                available += Convert.ToInt64(obj.Properties["AvailableMBytes"].Value);
            }
            sr.Append(available + "MB;");
            sr.AppendFormat("{0:F2}%可用; ", (double)available / (capacity / 1024 / 1024) * 100);

            return sr.ToString();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MemoryTs
{
    public class MemoryInfoEntity
    {
        /// <summary>
        /// 端口号
        /// </summary>
        public string PortNum { get; set; }

        /// <summary>
        /// 品名
        /// </summary>
        public string TradeName { get; set; }

        /// <summary>
        /// 容量
        /// </summary>
        public string Capacity { get; set; }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值