欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 获取机器硬件信息(cpu频率、磁盘可用空间、内存容量等)

程序员文章站 2024-02-06 19:05:46
复制代码 代码如下:using system; using system.data; using system.configuration; using system.we...

复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using microsoft.win32;
using system.text;
using system.runtime.interopservices;
using system.management;//(添加对 system.management.dll 的引用才能使用 system.management 命名空间)
namespace ec
{
/// <summary>
/// 收集机器硬件信息的相关代码片断(cpu频率、磁盘可用空间、内存容量……)
/// </summary>
public class cpuinfoobject
{
public cpuinfoobject()
{
//
// todo: 在此处添加构造函数逻辑
//
}
#region 获取cpu频率
/**************************************************
* 函数名称:getcpufrequency()
* 功能说明:获取cpu频率
* 参 数:
* 使用示列:
* response.write(ec.cpuinfoobject.getcpufrequency());
************************************************/
/// <summary>
/// 获取cpu频率
/// </summary>
/// <returns>整型cpu频率</returns>
public static int getcpufrequency()
{
registrykey rk = registry.localmachine.opensubkey(@"hardware\description\system\centralprocessor\0");
object obj = rk.getvalue("~mhz");
int cpufrequency = (int)obj;
return cpufrequency;
}
#endregion
#region 获取cpu名称
/**************************************************
* 函数名称:getcpuname()
* 功能说明:获取cpu名称
* 参 数:
* 使用示列:
* response.write(ec.cpuinfoobject.getcpuname());
************************************************/
/// <summary>
/// 获取cpu名称
/// </summary>
/// <returns>字符串型cpu名称</returns>
public static string getcpuname()
{
registrykey rk = registry.localmachine.opensubkey(@"hardware\description\system\centralprocessor\0");
object obj = rk.getvalue("processornamestring");
string cpuname = (string)obj;
return cpuname.trimstart();
}
#endregion
#region 磁盘空间
/**************************************************
* 函数名称:getfreediskspace(string diskname)
* 功能说明:获取磁盘空间
* 参 数:diskname:磁盘名称 d:或e:
* 使用示列:
* response.write(ec.cpuinfoobject.getfreediskspace("d:"));
************************************************/
/// <summary>
/// 磁盘空间
/// </summary>
/// <param name="diskname">硬盘名称:d:或e:</param>
/// <returns>整型</returns>
public static long getfreediskspace(string diskname)
{
managementobject disk = new managementobject("win32_logicaldisk.deviceid=\"" + diskname + "\"");
disk.get();
string totalbyte = disk["freespace"].tostring();
long freediskspacemb = convert.toint64(totalbyte) / 1024 / 1024;
return freediskspacemb;
}
#endregion
#region 获取操作系统版本
/**************************************************
* 函数名称:getosname()
* 功能说明:获取获取操作系统版本名称
* 参 数:
* 使用示列:
* response.write(ec.cpuinfoobject.getosname());
************************************************/
/// <summary>
/// 获取操作系统版本
/// </summary>
/// <returns>操作系统版本</returns>
public static string getosname()
{
string rev = "";
system.operatingsystem osinfo = system.environment.osversion;
switch(osinfo.platform)
{
//platform is windows 95, windows 98,windows 98 second edition, or windows me.
case system.platformid.win32windows:
switch(osinfo.version.major)
{
case 0:
rev ="windows 95";
break;
case 10:
if(osinfo.version.revision.tostring()=="2222a")
rev ="windows 98 second edition";
else
rev ="windows 98";
break;
case 90:
rev="windows me";
break;
}
break;
//platform is windows nt 3.51, windows nt 4.0, windows 2000,or windows xp.
case system.platformid.win32nt:
switch (osinfo.version.major)
{
case 3:
rev = "windows nt 3.51";
break;
case 4:
rev = "windows nt 4.0";
break;
case 5:
if (osinfo.version.minor == 0)
rev = "windows 2000";
else if (osinfo.version.minor == 2)
rev = "windows 2003";
else
rev = "windows xp";
break;
}
break;
}
return rev;
}
#endregion
}
}