asp.net 擷取機器硬體資訊(cpu頻率、磁碟可用空間、記憶體容量等)

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.