複製代碼 代碼如下: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
}
}