c# 獲取機器硬件信息 (硬盤,cpu,內存等)


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Management;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;

namespace HuaTong.General.Utility
{
    internal enum WmiType
    {
        Win32_Processor,
        Win32_PerfFormattedData_PerfOS_Memory,
        Win32_PhysicalMemory,
        Win32_NetworkAdapterConfiguration,
        Win32_LogicalDisk
    }

    /// <summary>
    /// 獲取硬盤號和CPU號
    /// </summary>
    public class MachineNumber
    {
        static Dictionary<string, ManagementObjectCollection> WmiDict = new Dictionary<string, ManagementObjectCollection>();

        static MachineNumber()
        {
            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);
                }
            }
            return sr.ToString();
        }

        /// <summary>
        /// 獲取操作系統信息
        /// </summary>
        /// <returns></returns>
        public static string OSInfo()
        {
            StringBuilder sr = new StringBuilder();
            sr.AppendFormat("機器名:{0};操作系統:{1};系統文件夾:{2};語言:{3};.NET:{4};當前目錄:{5};當前用戶:{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("總物理內存:");
            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();
        }

        /// <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("廠商:" + obj["Manufacturer"] + ";");
                sr.Append("產品名稱:" + obj["Name"] + ";");
                sr.Append("最大頻率:" + obj["MaxClockSpeed"] + ";");
                sr.Append("當前頻率:" + obj["CurrentClockSpeed"] + ";");
            }

            return sr.ToString();
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM