zoukankan      html  css  js  c++  java
  • 获取电脑硬件信息 (硬盘,cpu,内存等)

    引用:using System.Management;

    1、电脑信息类

    #region 获取电脑信息类
    
    
        internal enum WmiType
        {
            Win32_Processor,
            Win32_PerfFormattedData_PerfOS_Memory,
            Win32_PhysicalMemory,
            Win32_NetworkAdapterConfiguration,
            Win32_LogicalDisk
        }
    
        public class Computer
        {
            static Dictionary<string, ManagementObjectCollection> WmiDict = new Dictionary<string, ManagementObjectCollection>();
    
            static Computer()
            {
    
                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 string GetHardDiskNumber()
            {
                var query = WmiDict[WmiType.Win32_LogicalDisk.ToString()];
                //var collection = query.Get();
    
                string result = string.Empty;
                foreach (var obj in query)
                {
                    result = obj["VolumeSerialNumber"].ToString();
                    break;
                }
    
                return result;
            }
    
            /// <summary>
            /// 获取CPU号码
            /// </summary>
            /// <returns></returns>
            public static string GetCPUNumber()
            {
                var query = WmiDict[WmiType.Win32_Processor.ToString()];
                //var collection = query.Get();
    
                string result = string.Empty;
                foreach (var obj in query)
                {
                    result = obj["Processorid"].ToString();
                    break;
                }
    
                return result;
            }
    
            /// <summary>
            /// 获取内存编号
            /// </summary>
            /// <returns></returns>
            public static string GetMemoryNumber()
            {
                var query = WmiDict[WmiType.Win32_PhysicalMemory.ToString()];
                //var collection = query.Get();
    
                string result = string.Empty;
                foreach (var obj in query)
                {
                    result = obj["PartNumber"].ToString();
                    break;
                }
                return result;
            }
    
            /// <summary>
            /// 获取硬盘信息
            /// </summary>
            /// <returns></returns>
            public static string HardDiskInfo()
            {
                DriveInfo[] drives = DriveInfo.GetDrives();
                StringBuilder sr = new StringBuilder();
                foreach (DriveInfo drive in drives)
                {
                    if (drive.IsReady)
                    {
                        var val1 = (double)drive.TotalSize / 1024 / 1024;
                        var val2 = (double)drive.TotalFreeSpace / 1024 / 1024;
                        //sr.AppendFormat("{0}:{2}/{3}MB/{4}MB/{1}%可用;",   
                        //    drive.Name,
                        //    string.Format("{0:F2}", val2 / val1 * 100),
                        //    drive.DriveFormat,
                        //    (long)val1,
                        //    (long)val2);
    
                        sr.AppendFormat("<br>{0}:{2}/共:{3}GB/可用:{4}GB/{1}%可用;",   //GB
                           drive.Name,
                           string.Format("{0:F2}", val2 / val1 * 100),
                           drive.DriveFormat,
                           string.Format("{0:F1}",   val1/1024),
                           string.Format("{0:F1}", val2 / 1024));
                    }
                }
                return sr.ToString();
            }
    
            /// <summary>
            /// 获取操作系统信息
            /// </summary>
            /// <returns></returns>
            public static string OSInfo()
            {
                StringBuilder sr = new StringBuilder();
                sr.AppendFormat("<br>机器名:{0};<br>操作系统:{1};<br>系统文件夹:{2};<br>语言:{3};<br>.NET:{4};<br>当前目录:{5};<br>当前用户:{6};",
                    Environment.MachineName,
                    Environment.OSVersion,
                    Environment.SystemDirectory,
                    CultureInfo.InstalledUICulture.EnglishName,
                    Environment.Version,
                    Environment.CurrentDirectory,
                    Environment.UserName);
                return sr.ToString();
            }
    
            /// <summary>
            /// 获取网卡信息
            /// </summary>
            /// <returns></returns>
            //public static string NetworkInfo()
            //{
            //    StringBuilder sr = new StringBuilder();
    
            //    string host = Dns.GetHostName();
            //    IPHostEntry ipEntry = Dns.GetHostByName(host);
            //    sr.Append("IPv4:" + ipEntry.AddressList[0] + "/");
    
            //    sr.Append("IPv6:");
            //    ipEntry = Dns.GetHostEntry(host);
            //    sr.Append("IPv6:" + ipEntry.AddressList[0] + ";");
    
            //    sr.Append("MAC:");
            //    var query = WmiDict[WmiType.Win32_NetworkAdapterConfiguration.ToString()];
            //    foreach (var obj in query)
            //    {
            //        if (obj["IPEnabled"].ToString() == "True")
            //            sr.Append(obj["MacAddress"] + ";");
            //    }
    
            //    return sr.ToString();
            //}
    
            /// <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("<br>总物理内存:");
                //sr.Append(capacity / 1024 / 1024 + "MB;");  MB
                sr.Append(string.Format("{0:F1}", capacity / 1024 / 1024 / 1024) + "GB;");  //GB
                
    
                query = WmiDict[WmiType.Win32_PerfFormattedData_PerfOS_Memory.ToString()];
                sr.Append("<br>总可用内存:");
                long available = 0;
                foreach (var obj in query)
                {
                    available += Convert.ToInt64(obj.Properties["AvailableMBytes"].Value);
                }
                //sr.Append(available + "MB;");  //MB
                sr.Append(string.Format("{0:F1}", available / 1024) + "GB;");  //GB
                sr.AppendFormat("<br>{0:F2}%可用; ", (double)available / (capacity / 1024 / 1024) * 100);
    
                return sr.ToString();
            }
    
            /// <summary>
            /// 获取CPU信息
            /// </summary>
            /// <returns></returns>
            public static string CpuInfo()
            {
                StringBuilder sr = new StringBuilder();
    
                var query = WmiDict[WmiType.Win32_Processor.ToString()];
                foreach (var obj in query)
                {
                    sr.Append("<br>厂商:" + obj["Manufacturer"] + ";");
                    sr.Append("<br>产品名称:" + obj["Name"] + ";");
                    sr.Append("<br>最大频率:" + obj["MaxClockSpeed"] + ";");
                    sr.Append("<br>当前频率:" + obj["CurrentClockSpeed"] + ";");
                }
    
                return sr.ToString();
            }
        }
    
    
        #endregion
    获取电脑信息类

    2、方法调用

    string GetCPUNumber = Computer.GetCPUNumber();  //获取CPU号码
                string CpuInfo = Computer.CpuInfo();  //获取CPU信息
    
                string GetHardDiskNumber = Computer.GetHardDiskNumber();  //获取硬盘号码
                string HardDiskInfo = Computer.HardDiskInfo();  //获取硬盘信息
    
                string GetMemoryNumber = Computer.GetMemoryNumber();   //获取内存编号
                string MemoryInfo = Computer.MemoryInfo();  //获取内存信息
    
                string OSInfo = Computer.OSInfo();    //获取操作系统信息
    
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("<br/>当前电脑配置信息:" + "<br/>");
                sb.AppendFormat("<br/>获取CPU号码:{0}" + "<br/>", GetCPUNumber);
                sb.AppendFormat("<br/>获取CPU信息:{0}" + "<br/>", CpuInfo);
                sb.AppendFormat("<br/>获取硬盘号码:{0}" + "<br/>", GetHardDiskNumber);
                sb.AppendFormat("<br/>获取硬盘信息:{0}" + "<br/>", HardDiskInfo);
                sb.AppendFormat("<br/>获取内存编号:{0}" + "<br/>", GetCPUNumber);
                sb.AppendFormat("<br/>获取内存信息:{0}" + "<br/>", MemoryInfo);
                sb.AppendFormat("<br/>获取操作系统信息:{0}" + "<br/>", OSInfo);
    
                return Content(sb.ToString());

    3、运行效果如图:

  • 相关阅读:
    【转】减少缺陷漏测的系统方法体系思考(10年经验的反思)
    【转】如何一直保持测试工作的热情
    Airbnb创始人:屌丝的逆袭之路
    浅谈android应用性能之内存(转)
    【转】Android自动化测试之MonkeyRunner录制和回放脚本(四)
    【转】android自动化测试之MonkeyRunner使用实例(三)
    转 Android自动测试之monkeyrunner工具(二)
    monkeyrunner工具同Monkey工具的差别
    【转】monkeyrunner学习总结二:连接、安装、启动
    正则表达式全局匹配网址
  • 原文地址:https://www.cnblogs.com/sharing1986687846/p/10283584.html
Copyright © 2011-2022 走看看