C #2.0 obtain server CPU and memory usage

Source: Internet
Author: User
Using Microsoft. Win32;

Private int getcpufrequency ()
{
Registrykey rk = registry. localmachine. opensubkey (@ "Hardware \ description \ System \ centralprocessor \ 0 ");

Object OBJ = rk. getvalue ("~ MHz ");
Int cpufrequency = (INT) OBJ;
Return cpufrequency;
}
//////////////////////////////////

// Disk space management

Using system. Management;

Private long getfreediskspace ()
{
Managementobject disk = new managementobject (
"Win32_logicaldisk.deviceid = \" D :\"");
Disk. Get ();
String totalbyte = disk ["freespace"]. tostring ();
Long freediskspacemb = converter. toint64 (totalbyte)/1024/1024;
Return freediskspacemb;
}

/////////////////////
// Memory information

Using system;
Using system. text;
Using system. runtime. interopservices;

Namespace consoleapplication1
{
/** // <Summary>
/// Summary description for class1.
/// </Summary>

Class class1
{
[Structlayout (layoutkind. Sequential)]
Public struct memory_info
{
Public uint dwlength;
Public uint dwmemoryload;
Public uint dwtotalphys;
Public uint dwavailphys;
Public uint dwtotalpagefile;
Public uint dwavailpagefile;
Public uint dwtotalvirtual;
Public uint dwavailvirtual;
}
[Dllimport ("Kernel32")]
Public static extern void globalmemorystatus (ref memory_info meminfo );

Public static int main (string [] ARGs)
{
Class1 class1 = new class1 ();

Class1.getmemorystatus ();
Return 0;
}
Private void getmemorystatus ()
{
Memory_info meminfo;
Meminfo = new memory_info ();
Globalmemorystatus (ref meminfo );

Long totalmb = convert. toint64 (meminfo. dwtotalphys. tostring ()/1024/1024;
Long avaliablemb = convert. toint64 (meminfo. dwavailphys. tostring ()/1024/1024;

Console. writeline ("total physical memory" + totalmb + "MB ");
Console. writeline ("available physical memory" + avaliablemb + "MB ");
}

}
//////////////////////////////

// CPU name

Using Microsoft. Win32;
Private string getcpuname ()
{
Registrykey rk = registry. localmachine. opensubkey (@ "Hardware \ description \ System \ centralprocessor \ 0 ");

Object OBJ = rk. getvalue ("processornamestring ");
String cpuname = (string) OBJ;
Return cpuname. trimstart ();
}

///////////////////////
// OS version

Using system;

Namespace determineos_cs
{
Class class1
{
Static void main (string [] ARGs)
{
// Get operatingsystem information from the system namespace.
System. operatingsystem osinfo = system. environment. osversion;

// Determine the platform.
Switch (osinfo. Platform)
{
// Platform is Windows 95, Windows 98,
// Windows 98 Second edition, or Windows ME.
Case System. platformid. win32windows:

Switch (osinfo. version. Minor)
{
Case 0:
Console. writeline ("Windows 95 ");
Break;
Case 10:
If (osinfo. version. Revision. tostring () = "2222a ")
Console. writeline ("Windows 98 Second Edition ");
Else
Console. writeline ("Windows 98 ");
Break;
Case 90:
Console. writeline ("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:
Console. writeline ("Windows NT 3.51 ");
Break;
Case 4:
Console. writeline ("Windows NT 4.0 ");
Break;
Case 5:
If (osinfo. version. Minor = 0)
Console. writeline ("Window 2000 ");
Else
Console. writeline ("Windows XP ");
Break;
} Break;
}
Console. Readline ();
}
}
}

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.