Copy Code code as follows:
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;//(Adding a reference to System.Management.dll to use the System.Management namespace)
Namespace EC
{
<summary>
Collection of machine hardware information related code snippets (CPU frequency, disk free space, memory capacity ...) )
</summary>
public class Cpuinfoobject
{
Public Cpuinfoobject ()
{
//
TODO: Add constructor logic here
//
}
#region Get CPU Frequency
/**************************************************
* Function Name: getcpufrequency ()
* Function Description: Get CPU Frequency
Parameters
* Using columns:
* Response.Write (EC. Cpuinfoobject.getcpufrequency ());
************************************************/
<summary>
Get CPU Frequency
</summary>
<returns> Integer CPU Frequency </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 Get the CPU name
/**************************************************
* Function Name: Getcpuname ()
* Function Description: Get CPU Name
Parameters
* Using columns:
* Response.Write (EC. Cpuinfoobject.getcpuname ());
************************************************/
<summary>
Get CPU Name
</summary>
<returns> string-CPU name </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 Disk space
/**************************************************
* Function Name: Getfreediskspace (String diskname)
* Function Description: Get disk space
* Parameter: DiskName: Disk name D: or E:
* Using columns:
* Response.Write (EC. Cpuinfoobject.getfreediskspace ("D:"));
************************************************/
<summary>
Disk space
</summary>
<param name= "DiskName" > Hard disk Name: D: or e:</param>
<returns> Integral type </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 get the operating system version
/**************************************************
* Function Name: Getosname ()
* Function Description: Get the operating system version name
Parameters
* Using columns:
* Response.Write (EC. Cpuinfoobject.getosname ());
************************************************/
<summary>
Get the operating system version
</summary>
<returns> OS version </returns>
public static string Getosname ()
{
String Rev = "";
System.operatingsystem osInfo = System.Environment.OSVersion;
Switch (osinfo.platform)
{
Platform is windows, Windows 98,windows 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 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
}
}