我是靠谱客的博主 深情柠檬,最近开发中收集的这篇文章主要介绍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
      {
      }
    }
  }
}

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

最后

以上就是深情柠檬为你收集整理的C#如何获取计算机信息的全部内容,希望文章能够帮你解决C#如何获取计算机信息所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(104)

评论列表共有 0 条评论

立即
投稿
返回
顶部