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
      {
      }
    }
  }
}

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

(0)

相关推荐

  • C#读取计算机CPU及HDD信息的方法

    本文实例讲述了C#读取计算机CPU及HDD信息的方法.分享给大家供大家参考.具体如下: 这里使用C#读取计算机CPU,HDD信息,适用于Windows public string getCpuInfo() //读取CPU信息 { ManagementClass mobj = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mobj.GetInstances(); foreach (Mana

  • C#获取计算机名,IP,MAC信息实现代码

    利用C#获取计算机名,IP,MAC信息,如下为源代码: 复制代码 代码如下: using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Management; namespace Wenanry.Net { /// <summary> /// 获取计算机系统信息 /// </summary> public class ManagementSyst

  • C#编程获取客户端计算机硬件及系统信息功能示例

    本文实例讲述了C#编程获取客户端计算机硬件及系统信息功能.分享给大家供大家参考,具体如下: 这里使用C#获取客户端计算机硬件及系统信息 ,包括CPU.硬盘.IP.MAC地址.操作系统等. 1.项目引用System.Management库. 2.创建HardwareHandler.cs类文件 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Manag

  • 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> /// Compute

  • C#获取串口列表实现实时监控串口

    常用的两种方法 方法一: using Microsoft.Win32; RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware//DeviceMap//SerialComm"); if (keyCom != null) { string[] sSubKeys = keyCom.GetValueNames(); foreach (string sName in sSubKeys) { string sValue =

  • C#获取计算机硬件与操作系统的相关信息

    1.项目引用System.Management库文件 2.创建HardwareHandler.cs类文件 namespace HardInfoTest.Utility { /// <summary> /// 计算机硬件处理类 /// </summary> public class HardwareHandler { public enum WMIPath { // 硬件 Win32_Processor, // CPU 处理器 Win32_PhysicalMemory, // 物理内

  • php操作mysql获取select 结果的几种方法

    如果用了 MYSQL_BOTH,将得到一个同时包含关联和数字索引的数组. 用 MYSQL_ASSOC 只得到关联索引(如同mysql_fetch_assoc() 那样), 用 MYSQL_NUM 只得到数字索引(如同 mysql_fetch_row 那样). 1. mysql_fetch_array($rs,MYSQL_ASSOC) [@test01 model]# php test.php Array ( [name] => hellokitty [addr] => i dont kno )

  • 利用百度地图API获取当前位置信息的实例

    利用百度地图API可以做很多事情,个人感觉最核心也是最基础的就是定位功能了.这里分享一个制作的JS可以实现登录网页后定位: <script type="text/javascript"> var map; var gpsPoint; var baiduPoint; var gpsAddress; var baiduAddress; var x; var y; function getLocation() { //根据IP获取城市 var myCity = new BMap.

  • Spring获取ApplicationContext对象工具类的实现方法

     Spring获取ApplicationContext对象工具类的实现方法 (1)实现的工具类: package com.util; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; final public class ApplicationContextUtil { private s

  • 前端获取http状态码400的返回值实例

    如下所示: axios.get("/check_mobile_and_sent_code",{withCredentials:true,params:{mobile:formInline.mobile}}).then(res=>{ console.log(res); //if(res.result==true){ if (!this.timer) { this.count = this.TIME_COUNT; this.show = false; this.timer = set

  • vba 获取PPT幻灯片中的所有标题的代码

    复制代码 代码如下: Private Sub CommandButton1_Click() Me.Enabled = False getTitles Me.Enabled = True End Sub Sub getTitles() On Error Resume Next Dim oPres As Presentation Set oPres = Application.ActivePresentation Dim oSlide As Slide Dim oShape As Shape Dim

  • 获取外网IP并发送到指定的邮箱的脚本

    我们编译后的exe文件,方便大家直接使用 下载地址 http://www.jb51.net/softs/44627.html 配置文件内容如下: 复制代码 代码如下: 配置文件代码 [收件人] 收件地址= 邮件标题= [发送人] 邮箱= 密码= 核心代码: 复制代码 代码如下: #region AutoIt3Wrapper 预编译参数(常用参数) #AutoIt3Wrapper_Icon=D:\ico\3444\2222.ico ;图标,支持EXE,DLL,ICO #AutoIt3Wrapper

随机推荐