asp.net中获取本机的相关信息!(CPU、内存、硬盘序列号等)
// 注意:首先要在项目bin目录中添加引用 System.Management
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Management;
/// <summary>
///Computer 的摘要说明
/// </summary>
public class Computer
{
public string CpuID; //CPU的ID
public int CpuCount; //CPU的个数
public string[] CpuMHZ;//CPU频率 单位:hz
public string MacAddress;//计算机的MAC地址
public string DiskID;//硬盘的ID
public string DiskSize;//硬盘大小 单位:bytes
public string IpAddress;//计算机的IP地址
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();
CpuCount = GetCpuCount();
CpuMHZ = GetCpuMHZ();
MacAddress = GetMacAddress();
DiskID = GetDiskID();
DiskSize = GetSizeOfDisk();
IpAddress = GetIPAddress();
LoginUserName = GetUserName();
SystemType = GetSystemType();
TotalPhysicalMemory = GetTotalPhysicalMemory();
ComputerName = GetComputerName();
}
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
{
}
}
public static int GetCpuCount()
{
try
{
using (ManagementClass mCpu = new ManagementClass("Win32_Processor"))
{
ManagementObjectCollection cpus = mCpu.GetInstances();
return cpus.Count;
}
}
catch
{
}
return -1;
}
public static string[] GetCpuMHZ()
{
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection cpus = mc.GetInstances();
string[] mHz = new string[cpus.Count];
int c = 0;
ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor");
foreach (ManagementObject mo in mySearch.Get())
{
mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString();
c++;
}
mc.Dispose();
mySearch.Dispose();
return mHz;
}
public static string GetSizeOfDisk()
{
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moj = mc.GetInstances();
foreach (ManagementObject m in moj)
{
return m.Properties["Size"].Value.ToString();
}
return "-1";
}
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
{
}
}
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
{
}
}
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>
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
{
}
}
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
{
}
}
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
{
}
}
string GetComputerName()
{
try
{
return System.Environment.GetEnvironmentVariable("ComputerName");
}
catch
{
return "unknow ";
}
finally
{
}
}
}
asp.net中获取本机的相关信息!(CPU、内存、硬盘序列号等)的更多相关文章
- DSAPI 获取实时统计信息CPU/内存/硬盘/网络
有时,我们需要获取当前计算机中CPU.内存.硬盘.网络等实时信息,如下图:\ 要实现上述几项信息的获取,通常需要使用Timer控件来间隔获取,以便刷新最新的数据. 本示例中,放一个Timer控件,放一 ...
- ASP.NET中获取当日,当周,当月,当年的日期
ASP.NET中获取当日,当周,当月,当年的日期 在ASP.NET开发中,经常会碰到要获取当日,当周,当月,当年的日期. 以下将源码贴出来和大家分享. aspx中代码如下: <table ce ...
- asp.net中获取当前url的方法
HttpContext.Current.Request.Url.ToString() 并不可靠. 如果当前URL为 http://localhost/search.aspx?user=http://c ...
- shell中获取本机ip地址
shell中获取本机ip地址 方法一: /sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr ...
- Linux中获取本机的最新IPv6地址_更新ddns的脚本
Linux中获取本机的最新IPv6地址_更新ddns的脚本 转载注明来源: 本文链接 来自osnosn的博客,写于 2019-11-07. 运营商提供ipv6地址. 路由器后有台linux机器,通过e ...
- RTP中的H264的SVC相关信息
概览 本文简单的介绍了rtp中的h264的svc相关信息 h264的svc相关格式信息 从RTP Payload Format for H.264 Video可以看见264在rtp的传输过程中,在rt ...
- linux编程获取本机网络相关参数
getifaddrs()和struct ifaddrs的使用,获取本机IP 博客分类: Linux C编程 ifaddrs结构体定义如下: struct ifaddrs { struct ifad ...
- Linux中获取本机网络信息的几个函数及应用
一.读取/etc/hosts 几个函数 头文件<netdb.h> 1.void sethostent(int stayopen);//开打/etc/hosts 配置文件 2.struct ...
- Android中获取应用程序(包)的信息----PackageManager
本节内容是如何获取Android系统中应用程序的信息,主要包括packagename.label.icon.占用大小等.具体分为两个 部分,计划如下: 第一部分: 获取应用程序的packagena ...
随机推荐
- 商业智能BI
参考资料: 7 款顶级开源 BI(商务智能)软件和报表工具
- JAVA动态加载JAR
// 生成JAR包D:\TestClass.jar package hand.java.loadjar; public class TestClass { private String sayHell ...
- win7下IIS的安装和配置 图文教程
转自 http://www.jb51.net/article/29787.htm 最近工作需要IIS,自己的电脑又是Windows7系统,找了下安装的方法,已经安装成功.在博客里记录一下,给需要的 ...
- (转)winform(C#)里几种弹出对话框
//消息框中需要显示哪些按钮,此处显示“确定”和“取消” MessageBoxButtons messButton = MessageBoxButtons.OKCancel; //"确定要退 ...
- 超详细Web前端开发规范文档
http://www.w3cfuns.com/notes/26488/c2ae788c77f835357025026a148b9863.html
- saltstack命令执行过程
saltstack命令执行过程 具体步骤如下 Salt stack的Master与Minion之间通过ZeroMq进行消息传递,使用了ZeroMq的发布-订阅模式,连接方式包括tcp,ipc salt ...
- markdown编辑器sublime text3
安装包管理Package Control ctrl+`或者点击查看-->显示面板,复制下面的代码到面板里. import urllib.request,os; pf = 'Package Con ...
- 触发bfd 的条件
满足下列条件之一就可触发BFC [1]根元素,即HTML元素 [2]float的值不为none [3]overflow的值不为visible [4]display的值为inline-block.tab ...
- Unity3D访问Android系统目录
file:///sdcard/Movies/3D/互动.mp4file:///storage/emulated/0/Movies/3D/互动.mp4/storage/emulated/0/Movies ...
- 简化MSI在WIN10的安装
这里给大家分享一个简化MSI安装的工具 InstallByDrag: 在win10系统中,通过双击方式打开 MSI 安装文件,可能被提示由于dll加载问题无法安装,这是由于没有使用管理员权限运行.而M ...