C # Use APIs to view system information

Source: Internet
Author: User
<HTML>
<Head>
<Title> systeminfo </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Colgroup>
& Lt; Col width = "50%" & gt;
& Lt; Col width = "50%" & gt;
</Colgroup>
& Lt; table width = "100%" & gt;
<Tr>
<TD bgcolor = "#99 CCFF"> window and System Path: </TD>
<TD bgcolor = "#99 CCFF"> system date information: </TD>
</Tr>
<Tr>
<TD>
<Asp: Label id = "windowsdirectory" runat = "server"> windowsdirectory </ASP: Label> </TD>
<TD>
<Asp: Label id = "date" runat = "server"> date </ASP: Label> </TD>
</Tr>
<Tr>
<TD>
<Asp: Label id = "systemdirectory" runat = "server"> systemdirectorylabel </ASP: Label> </TD>
<TD>
<Asp: Label id = "time" runat = "server"> time </ASP: Label> </TD>
</Tr>
<Tr>
<TD bgcolor = "#99 CCFF"> memory information: </TD>
<TD bgcolor = "#99 CCFF"> cup information: </TD>
</Tr>
<Tr>
<TD>
<Asp: Label id = "memoryload" runat = "server"> memoryload </ASP: Label> </TD>
<TD>
<Asp: Label id = "numberofprocessors" runat = "server"> numberofprocessorslabel </ASP: Label> </TD>
</Tr>
<Tr>
<TD>
<Asp: Label id = "totalphys" runat = "server"> totalphys </ASP: Label> </TD>
<TD>
<Asp: Label id = "processortype" runat = "server"> processortypelabel </ASP: Label> </TD>
</Tr>
<Tr>
<TD>
<Asp: Label id = "availphys" runat = "server"> availphys </ASP: Label> </TD>
<TD>
<Asp: Label id = "processorlevel" runat = "server"> processorlevellabel </ASP: Label> </TD>
</Tr>
<Tr>
<TD>
<Asp: Label id = "totalpagefile" runat = "server"> totalpagefile </ASP: Label> </TD>
<TD>
<Asp: Label id = "oemid" runat = "server"> oemidlabel </ASP: Label> </TD>
</Tr>
<Tr>
<TD>
<Asp: Label id = "availpagefile" runat = "server"> availpagefile </ASP: Label> </TD>
<TD>
<Asp: Label id = "pagesize" runat = "server"> pagesize </ASP: Label> </TD>
</Tr>
<Tr>
<TD>
<Asp: Label id = "totalvirtual" runat = "server"> totalvirtuallabel </ASP: Label> </TD>
<TD> </TD>
</Tr>
<Tr>
<TD>
<Asp: Label id = "availvirtual" runat = "server"> availpolicallabel </ASP: Label> </TD>
<TD> </TD>
</Tr>
</Table>
<Asp: button id = "button1" style = "Z-INDEX: 101; left: 752px; position: absolute; top: 304px" runat = "server"
TEXT = "get"> </ASP: button>
</Form>
</Body>
</Html>

Public class systeminfo: system. Web. UI. Page
{
// Add the following reference
// Using system. runtime. interopservices;
// Using system. text;
Protected system. Web. UI. webcontrols. Label windowsdirectory;
Protected system. Web. UI. webcontrols. Label systemdirectory;
Protected system. Web. UI. webcontrols. Label numberofprocessors;
Protected system. Web. UI. webcontrols. Label processortype;
Protected system. Web. UI. webcontrols. Label processorlevel;
Protected system. Web. UI. webcontrols. Label oemid;
Protected system. Web. UI. webcontrols. Label pagesize;
Protected system. Web. UI. webcontrols. Label memoryload;
Protected system. Web. UI. webcontrols. Label totalphys;
Protected system. Web. UI. webcontrols. Label availphys;
Protected system. Web. UI. webcontrols. Label totalpagefile;
Protected system. Web. UI. webcontrols. Label availpagefile;
Protected system. Web. UI. webcontrols. Label totalvirtual;
Protected system. Web. UI. webcontrols. Label availvirtual;
Protected system. Web. UI. webcontrols. Label date;
Protected system. Web. UI. webcontrols. Label time;
Protected system. Web. UI. webcontrols. Button button1;

// Before calling an API, you must first import the system. runtime. interopservices namespace.
// The "dllimport" attribute is used to call a method from uncontrollable code. It specifies the DLL location and contains the called external method;
// "Kernel32" sets the class library name; "public" indicates that the function access type is public;
// The "static" modifier declares a static element, which belongs to the type rather than the specified object;
// "Extern" indicates that this method will be executed outside the project, and the method imported using dllimport must use the "extern" Modifier
[Dllimport ("Kernel32")]
Public static extern void getwindowsdirectory (stringbuilder WINDIR, int count );
[Dllimport ("Kernel32")]
Public static extern void getsystemdirectory (stringbuilder Sysdir, int count );
[Dllimport ("Kernel32")]
Public static extern void getsysteminfo (ref cpu_info cpuinfo );
[Dllimport ("Kernel32")]
Public static extern void globalmemorystatus (ref memory_info meminfo );
[Dllimport ("Kernel32")]
Public static extern void getsystemtime (ref systemtime_info stinfo );

// Define the CPU Information Structure
[Structlayout (layoutkind. Sequential)]
Public struct cpu_info
{
Public uint dwoemid;
Public uint dwpagesize;
Public uint lpminimumapplicationaddress;
Public uint lpmaximumapplicationaddress;
Public uint dwactiveprocessormask;
Public uint dwnumberofprocessors;
Public uint dwprocessortype;
Public uint dwallocationgranularity;
Public uint dwprocessorlevel;
Public uint dwprocessorrevision;
}
// Define the memory information structure
[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;
}
// Define the system time information structure
[Structlayout (layoutkind. Sequential)]
Public struct systemtime_info
{
Public ushort wyear;
Public ushort wmonth;
Public ushort wdayofweek;
Public ushort wday;
Public ushort whour;
Public ushort wminute;
Public ushort wsecond;
Public ushort wmilliseconds;
}

Private void page_load (Object sender, system. eventargs E)
{
// Put user code to initialize the page here
}

Web Form Designer generated code

Private void button#click (Object sender, system. eventargs E)
{
// Call the getwindowsdirectory and getsystemdirectory functions to obtain the Windows path and system path respectively.
Const int nchars = 128;
Stringbuilder buff = new stringbuilder (nchars );
Getwindowsdirectory (buff, nchars );
Windows directory. Text = "Windows path:" + buff. tostring ();
Getsystemdirectory (buff, nchars );
Systemdirectory. Text = "System Path:" + buff. tostring ();
// Call the getsysteminfo function to obtain CPU Information
Cpu_info cpuinfo;
Cpuinfo = new cpu_info ();
Getsysteminfo (ref cpuinfo );
Numberofprocessors. Text = "in this computer" + cpuinfo. dwnumberofprocessors. tostring () + "CPU ";
Processortype. Text = "CPU type" + cpuinfo. dwprocessortype. tostring ();
Processorlevel. Text = "CPU level" + cpuinfo. dwprocessorlevel. tostring ();
Oemid. Text = "cpu oem id is" + cpuinfo. dwoemid. tostring ();
Pagesize. Text = "page size in CPU is" + cpuinfo. dwpagesize. tostring ();
// Call the globalmemorystatus function to obtain memory information.
Memory_info meminfo;
Meminfo = new memory_info ();
Globalmemorystatus (ref meminfo );
Memoryload. Text = meminfo. dwmemoryload. tostring () + "% memory in use ";
Totalphys. Text = "total physical memory" + meminfo. dwtotalphys. tostring () + "Byte ";
Availphys. Text = "available physical memory" + meminfo. dwavailphys. tostring () + "Byte ";
Totalpagefile. Text = "the total size of the swap file is" + meminfo. dwtotalpagefile. tostring () + "Byte ";
Availpagefile. Text = "the size of the interchangeable file is" + meminfo. dwavailpagefile. tostring () + "Byte ";
Totalvirtual. Text = "Total virtual memory" + meminfo. dwtotalvirtual. tostring () + "Byte ";
Availvirtual. Text = "unused Virtual Memory" + meminfo. dwavailvirtual. tostring () + "Byte ";
// Call the getsystemtime function to obtain the system time information.
Systemtime_info stinfo;
Stinfo = new systemtime_info ();
Getsystemtime (ref stinfo );
Date. Text = stinfo. wyear. tostring () + "year" + stinfo. wmonth. tostring () + "month" + stinfo. wday. tostring () + "day ";
Time. TEXT = (stinfo. whour + 8 ). tostring () + "point" + stinfo. wminute. tostring () + "Minute" + stinfo. wsecond. tostring () + "seconds ";
}
}

 

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.