C#如何获取计算机信息
作者:gmadc 时间:2022-08-24 23:59:34
本文实例为大家分享了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
{
}
}
}
}
来源:https://blog.csdn.net/gmadc/article/details/107077831
标签:C#,计算机
0
投稿
猜你喜欢
java清除html转义字符
2023-03-18 12:20:10
SpringBoot中JPA实现Sort排序的三种方式小结
2022-02-12 23:35:12
Android ImageView实现图片裁剪和显示功能
2022-01-31 10:35:22
C# 中AutoMapper的使用方法
2023-04-06 13:17:04
Java杂谈之重复代码是什么
2022-06-08 15:07:24
C#实现两个richtextbox控件滚动条同步滚动的简单方法
2023-10-14 19:34:51
浅析Android 模拟键盘鼠标事件
2022-12-19 00:11:14
Java实现AOP功能的封装与配置的小框架实例代码
2022-12-27 10:57:45
C# WebApi 路由机制剖析
2022-01-23 05:20:16
C#实现生成mac地址与IP地址注册码的两种方法
2022-07-14 20:14:58
解决genymotion模拟器无法联网的正确方法100%成功
2023-09-04 23:20:54
教你开发脚手架集成Spring Boot Actuator监控的详细过程
2021-11-19 03:27:39
一文带你搞懂Redis分布式锁
2021-09-26 12:56:14
Android设计模式之单例模式详解
2022-03-12 16:51:47
简单聊聊c# 事件
2022-03-05 06:16:52
SpringMVC的注解@RequestMapping属性及使用
2022-12-31 02:03:00
Java编程实现A*算法完整代码
2022-06-18 12:28:57
Android实现沉浸式状态栏功能
2022-10-25 23:19:36
Android支付宝支付开发实例
2023-01-01 09:15:18
springboot拦截器Interceptor的使用,你都了解吗
2023-01-01 21:53:40