C# 获取计算机相关信息

整理了一个关于计算机相关系统的资料

需要引入命名空间:

1. 在'解决方案资源管理器' 窗口中->右击项目-> '添加' -> '引用' 弹出引用管理器

2. 在引用处理器中,程序集-> 框架 -> 选中 'System.Management' -> 确认

代码语言:javascript
复制
using System;
using System.Management;
using System.IO;
代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.IO;

namespace WindowsFormsApp1
{
 /// <summary>
/// 计算机信息类
/// </summary>
 public class GetComputerInfo
{
public string CpuID;//Cpu编号
public string MacAddress;//Mac地址
public string DiskID;//磁盘ID
public string IpAddress;//IP地址
public string LoginUserName;//系统用户名
public string ComputerName;//计算机名称
public string SystemType;//系统类型
public string TotalPhysicalMemory; //单位:M
private static GetComputerInfo _instance;

    public static GetComputerInfo GetInstance()
    {
        if (_instance == null)
            _instance = new GetComputerInfo();
        return _instance;
    }
    public GetComputerInfo()
    {
        CpuID = GetCpuID();
        MacAddress = GetMacAddress();
        DiskID = GetDiskID();
        IpAddress = GetIPAddress();
        LoginUserName = GetUserName();
        SystemType = GetSystemType();
        TotalPhysicalMemory = GetTotalPhysicalMemory();
        ComputerName = GetComputerName();
    }
    /// &lt;summary&gt;
    /// 获取CPU的个数
    /// &lt;/summary&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static int GetCpuCount()
    {
        try
        {
            using (ManagementClass mCpu = new ManagementClass(&#34;Win32_Processor&#34;))
            {
                ManagementObjectCollection cpus = mCpu.GetInstances();
                return cpus.Count;
            }
        }
        catch
        {
        }
        return -1;
    }

   /// &lt;summary&gt;
   /// 获取CPU的频率 这里之所以使用string类型的数组,主要是因为cpu的多核
   /// &lt;/summary&gt;
   /// &lt;returns&gt;&lt;/returns&gt;
   public static string[] GetCpuMHZ()
    {
        ManagementClass mc = new ManagementClass(&#34;Win32_Processor&#34;);
        ManagementObjectCollection cpus = mc.GetInstances();
        string[] mHz = new string[cpus.Count];
        int c = 0;
        ManagementObjectSearcher mySearch = new ManagementObjectSearcher(&#34;select * from Win32_Processor&#34;);
        foreach (ManagementObject mo in mySearch.Get())
        {
            mHz[c] = mo.Properties[&#34;CurrentClockSpeed&#34;].Value.ToString();
            c++;
        }
        mc.Dispose();
        mySearch.Dispose();
        return mHz;
    }
    /// &lt;summary&gt;
    /// 获取本机硬盘的大小
    /// &lt;/summary&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static string GetSizeOfDisk()
    {
        ManagementClass mc = new ManagementClass(&#34;Win32_DiskDrive&#34;);
        ManagementObjectCollection moj = mc.GetInstances();
        foreach (ManagementObject m in moj)
        {
            return m.Properties[&#34;Size&#34;].Value.ToString();
        }
        return &#34;-1&#34;;
    }
    /// &lt;summary&gt;
    ///  获取本机内存的大小:
    /// &lt;/summary&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static string GetSizeOfMemery()
    {
        ManagementClass mc = new ManagementClass(&#34;Win32_OperatingSystem&#34;);
        ManagementObjectCollection moc = mc.GetInstances();
        double sizeAll = 0.0;
        foreach (ManagementObject m in moc)
        {
            if (m.Properties[&#34;TotalVisibleMemorySize&#34;].Value != null)
            {
                sizeAll += Convert.ToDouble(m.Properties[&#34;TotalVisibleMemorySize&#34;].Value.ToString());
            }
        }
        mc = null;
        moc.Dispose();
        return sizeAll.ToString();
    }
  
    /// &lt;summary&gt;
    /// 获取磁盘剩余空间
    /// &lt;/summary&gt;
    /// &lt;param name=&#34;str_HardDiskName&#34;&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    long GetHardDiskFreeSpace(string str_HardDiskName)
    {
        long num = 0L;
        str_HardDiskName = str_HardDiskName + @&#34;:\&#34;;
        foreach (DriveInfo info in DriveInfo.GetDrives())
        {
            if (info.Name.ToUpper() == str_HardDiskName.ToUpper())
            {
                num = info.TotalFreeSpace / 0x100000L;
            }
        }
        return num;
    }

    //获得CPU编号
    string GetCpuID()
    {
        try
        {
            //获取CPU序列号代码 
            string cpuInfo = &#34;&#34;;//cpu序列号 
            ManagementClass mc = new ManagementClass(&#34;Win32_Processor&#34;);
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuInfo = mo.Properties[&#34;ProcessorId&#34;].Value.ToString();
            }
            moc = null;
            mc = null;
            return cpuInfo;
        }
        catch
        {
            return &#34;unknow&#34;;
        }
    }
    //获得Mac地址
    string GetMacAddress()
    {
        try
        {
            //获取网卡硬件地址 
            string mac = &#34;&#34;;
            ManagementClass mc = new ManagementClass(&#34;Win32_NetworkAdapterConfiguration&#34;);
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo[&#34;IPEnabled&#34;] == true)
                {
                    mac = mo[&#34;MacAddress&#34;].ToString();
                    break;
                }
            }
            moc = null;
            mc = null;
            return mac;
        }
        catch
        {
            return &#34;unknow&#34;;
        }
    }
    //获得Ip地址
    string GetIPAddress()
    {
        try
        {
            //获取IP地址 
            string st = &#34;&#34;;
            ManagementClass mc = new ManagementClass(&#34;Win32_NetworkAdapterConfiguration&#34;);
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo[&#34;IPEnabled&#34;] == true)
                {
                    //st=mo[&#34;IpAddress&#34;].ToString(); 
                    System.Array ar;
                    ar = (System.Array)(mo.Properties[&#34;IpAddress&#34;].Value);
                    st = ar.GetValue(0).ToString();
                    break;
                }
            }
            moc = null;
            mc = null;
            return st;
        }
        catch
        {
            return &#34;unknow&#34;;
        }
    }
    //获得磁盘Id
    string GetDiskID()
    {
        try
        {
            //获取硬盘ID 
            String HDid = &#34;&#34;;
            ManagementClass mc = new ManagementClass(&#34;Win32_DiskDrive&#34;);
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                HDid = (string)mo.Properties[&#34;Model&#34;].Value;
            }
            moc = null;
            mc = null;
            return HDid;
        }
        catch
        {
            return &#34;unknow&#34;;
        }
    }
    /// &lt;summary&gt; 
    /// 操作系统的登录用户名 
    /// &lt;/summary&gt; 
    /// &lt;returns&gt;&lt;/returns&gt; 
    string GetUserName()
    {
        try
        {
            string st = &#34;&#34;;
            ManagementClass mc = new ManagementClass(&#34;Win32_ComputerSystem&#34;);
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                st = mo[&#34;UserName&#34;].ToString();
            }
            moc = null;
            mc = null;
            return st;
        }
        catch
        {
            return &#34;unknow&#34;;
        }
    }
    /// &lt;summary&gt; 
    /// PC类型 
    /// &lt;/summary&gt; 
    /// &lt;returns&gt;&lt;/returns&gt; 
    string GetSystemType()
    {
        try
        {
            string st = &#34;&#34;;
            ManagementClass mc = new ManagementClass(&#34;Win32_ComputerSystem&#34;);
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                st = mo[&#34;SystemType&#34;].ToString();
            }
            moc = null;
            mc = null;
            return st;
        }
        catch
        {
            return &#34;unknow&#34;;
        }
    }

    /// &lt;summary&gt; 
    /// 物理内存 
    /// &lt;/summary&gt; 
    /// &lt;returns&gt;&lt;/returns&gt; 
    string GetTotalPhysicalMemory()
    {
        try
        {
            string st = &#34;&#34;;
            ManagementClass mc = new ManagementClass(&#34;Win32_ComputerSystem&#34;);
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {

           st = mo["TotalPhysicalMemory"].ToString();
}
moc = null;
mc = null;
return st;
}
catch
{
return "unknow";
}
}
/// <summary>
/// 获取计算机名称
/// </summary>
/// <returns></returns>
string GetComputerName()
{
try
{
return System.Environment.GetEnvironmentVariable("ComputerName");
}
catch
{
return "unknow";
}
}
}
}

ok,今天分享到这了,如果有疑问的可以留言,讲的不对的欢迎指出!!!