C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#获取计算机信息

C#如何获取计算机信息

作者:gmadc

这篇文章主要为大家详细介绍了C#获取计算机信息的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#获取计算机信息的具体代码,供大家参考,具体内容如下

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
 
 
namespace test
{
  /// <summary>
  /// Computer informercial
  /// </summary>
  class Computer
  {
    public string CpuID;
    public string MacAddress;
    public string DiskID;
    public string IpAddress;
    public string LoginUserName;
    public string ComputerName;
    public string SystemType;
    public string TotalPhysicalMemory; //单位:M 
    private static Computer _instance;
 
 
    public static Computer Instance()
    {
      if (_instance == null)
        _instance = new Computer();
      return _instance;
    }
 
    public Computer()
    {
      CpuID = GetCpuID();
      MacAddress = GetMacAddress();
      DiskID = GetDiskID();
      IpAddress = GetIPAddress();
      LoginUserName = GetUserName();
      SystemType = GetSystemType();
      TotalPhysicalMemory = GetTotalPhysicalMemory();
      ComputerName = GetComputerName();
    }
    /// <summary>
    /// 获取CPU ID
    /// </summary>
    /// <returns></returns>
    private string GetCpuID()
    {
      try
      {
        //获取CPU序列号代码 
        string cpuInfo = "";//cpu序列号
        ManagementClass mc = new ManagementClass("Win32_Processor");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach(ManagementObject mo in moc)
        {
          cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
        }
        moc = null;
        mc = null;
 
        return cpuInfo;
 
      }
      catch
      {
        return "unknow";
      }
      finally
      {
 
      }
    }
    /// <summary>
    /// 获取Mac 地址
    /// </summary>
    /// <returns></returns>
    private string GetMacAddress()
    {
      
      try
      {
        //获取网卡硬件地址 
        string mac = "";
        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach(ManagementObject mo in moc)
        {
          if((bool)mo["IPEnabled"]==true)
          {
            mac = mo["MacAddress"].ToString();
            break;
          }
        }
        moc = null;
        mc = null;
        return mac;
 
      }
      catch
      {
        return "unknow";
      }
      finally
      {
 
      }
    }
    /// <summary>
    /// 获取 ip地址
    /// </summary>
    /// <returns></returns>
    private string GetIPAddress()
    {
      try
      {
        //获取IP地址 
        string st = "";
        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
          if ((bool)mo["IPEnabled"] == true)
          {
            //st=mo["IpAddress"].ToString(); 
            System.Array ar;
            ar = (System.Array)(mo.Properties["IpAddress"].Value);
            st = ar.GetValue(0).ToString();
            break;
          }
        }
        moc = null;
        mc = null;
        return st;
      }
      catch
      {
        return "unknow";
      }
      finally
      {
 
      }
    }
    /// <summary>
    /// 获取硬盘ID
    /// </summary>
    /// <returns></returns>
    private string GetDiskID()
    {
      try
      {
        //获取硬盘ID 
        String HDid = "";
        ManagementClass mc = new ManagementClass("Win32_DiskDrive");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
          HDid = (string)mo.Properties["Model"].Value;
        }
        moc = null;
        mc = null;
        return HDid;
      }
      catch
      {
        return "unknow";
      }
      finally
      {
      }
    }
    /// <summary>
    /// 用户名 
    /// </summary>
    /// <returns></returns>
    private string GetComputerName()
    {
      try
      {
        return System.Environment.GetEnvironmentVariable("ComputerName");
      }
      catch
      {
        return "unknow";
      }
      finally
      {
      }
    }
    /// <summary>
    /// 物理内存
    /// </summary>
    /// <returns></returns>
    private string GetTotalPhysicalMemory()
    {
      try
      {
 
        string st = "";
        ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
 
          st = mo["TotalPhysicalMemory"].ToString();
 
        }
        moc = null;
        mc = null;
        return st;
      }
      catch
      {
        return "unknow";
      }
      finally
      {
      }
    }
    /// <summary>
    /// PC类型 
    /// </summary>
    /// <returns></returns>
    private string GetSystemType()
    {
      try
      {
        string st = "";
        ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
 
          st = mo["SystemType"].ToString();
 
        }
        moc = null;
        mc = null;
        return st;
      }
      catch
      {
        return "unknow";
      }
      finally
      {
      }
    }
    /// <summary>
    /// 操作系统的登录用户名 
    /// </summary>
    /// <returns></returns>
    private string GetUserName()
    {
      try
      {
        string st = "";
        ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
 
          st = mo["UserName"].ToString();
 
        }
        moc = null;
        mc = null;
        return st;
      }
      catch
      {
        return "unknow";
      }
      finally
      {
      }
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文