Asp.net obtains machine hardware information (cpu frequency, disk space, memory capacity, etc)

Source: Internet
Author: User

Copy codeThe Code is 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; // (to use the System. Management namespace, add a reference to System. Management. dll)
Namespace EC
{
/// <Summary>
/// Code snippet related to collecting machine hardware information (cpu frequency, disk available space, memory capacity ......)
/// </Summary>
Public class CpuInfoObject
{
Public CpuInfoObject ()
{
//
// TODO: add the constructor logic here
//
}
# Region CPU acquisition frequency
/*************************************** ***********
* Function name: GetCPUFrequency ()
* Function Description: obtains the CPU frequency.
* Parameters:
* Use the display column:
* Response. Write (EC. CpuInfoObject. GetCPUFrequency ());
**************************************** ********/
/// <Summary>
/// Obtain the 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 obtain the CPU name
/*************************************** ***********
* Function name: GetCPUName ()
* Function Description: Obtain the CPU name.
* Parameters:
* Use the display column:
* Response. Write (EC. CpuInfoObject. GetCPUName ());
**************************************** ********/
/// <Summary>
/// Obtain the CPU name
/// </Summary>
/// <Returns> string-type 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: obtains the disk space.
* Parameter: DiskName: disk name D: Or E:
* Use the display column:
* Response. Write (EC. CpuInfoObject. GetFreeDiskSpace ("D :"));
**************************************** ********/
/// <Summary>
/// Disk space
/// </Summary>
/// <Param name = "DiskName"> hard disk name: D: Or E: </param>
/// <Returns> integer </returns>
Public static long GetFreeDiskSpace (string DiskName)
{
ManagementObject disk = new ManagementObject ("win32_logicaldisk.deviceid = \" "+ DiskName + "\"");
Disk. Get ();
String totalByte = disk ["FreeSpace"]. ToString ();
Long freeDiskSpaceMb = converter. ToInt64 (totalByte)/1024/1024;
Return freeDiskSpaceMb;
}
# Endregion
# Region obtain the operating system version
/*************************************** ***********
* Function name: GetOSName ()
* Function Description: Obtain the operating system version name.
* Parameters:
* Use the display column:
* Response. Write (EC. CpuInfoObject. GetOSName ());
**************************************** ********/
/// <Summary>
/// Obtain the OS version
/// </Summary>
/// <Returns> operating system version </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
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.