Method 1: Use the systeminformation class
SysteminformationProvides static methods and attributes that can be used to obtain information such as Windows display element size, operating system settings, network availability, and performance of the hardware installed in the system.Focuses on Operating Systems(Windows)Some settings and status. Method 2:EnvironmentClass
System information that can be retrieved through this method includes command line parameters, environment variable settings, call stack content, time since the last system boot, and version of the Public Language Runtime Library,The test focuses on the environment and status of applications using this class,There are also a lot of dynamic informationFor example, the number of milliseconds after startup. Method 3Use registrykeyClass
This method can only read static settings, but it is closer to the operating system than the above two methods, because the value is directly read from the registry, there are advantages and disadvantages. The advantage is that you can get any settings. It is not convenient to retrieve the registry key value. Method 4Use APIFunction
Strictly speaking, this method does not use C # To Read system information, because it actually calls the system API. This method is more meaningful, and is called for API functions, so we can do more. This method can also be used in Java. The following describes the program structure: // The package required by the registrykey class: Using Microsoft. Win32; // The package required by the dllimport method: Using system. runtime. interopservices; // The package required by stringbuilder: Using system. text; // Declare API functions [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 following structures // 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 initsysinfodata () { // Obtain operating system settings Lstsysinfo. Items. Add ("computer name:" + systeminformation. computername ); Lstsysinfo. Items. Add ("connected network:" + systeminformation. Network ); Lstsysinfo. Items. Add ("User Domain:" + systeminformation. userdomainname ); Lstsysinfo. Items. Add ("current thread Username:" + systeminformation. username ); Lstsysinfo. Items. Add ("Startup Mode:" + systeminformation. bootmode ); Lstsysinfo. Items. Add ("font of the menu:" + systeminformation. menufont ); Lstsysinfo. Items. Add ("number of monitors:" + systeminformation. monitorcount ); Lstsysinfo. Items. Add ("the mouse has been installed:" + systeminformation. mousepresent ); Lstsysinfo. Items. Add ("Number of mouse buttons:" + systeminformation. mousebuttons ); Lstsysinfo. Items. Add ("Interaction Mode:" + systeminformation. userinteractive ); Lstsysinfo. Items. Add ("screen boundary:" + systeminformation. virtualscreen ); } Public void initenvdata () {
// Obtain information about the program running. Lstenv. Items. Add ("command line:" + environment. CommandLine ); Lstenv. Items. Add ("command line parameter:" + String. Join (",", environment. getcommandlineargs ())); Lstenv. Items. Add ("Current Directory:" + environment. currentdirectory ); Lstenv. Items. Add ("operating system version:" + environment. osversion ); Lstenv. Items. Add ("system directory:" + environment. systemdirectory ); Lstenv. Items. Add ("stack information:" + environment. stacktrace ); Lstenv. Items. Add ("User Domain:" + environment. userdomainname ); Lstenv. Items. Add ("CLR version:" + environment. version ); Lstenv. Items. Add ("milliseconds after system startup:" + environment. tickcount ); Lstenv. Items. Add ("physical memory size of process context:" + environment. workingset ); String [] drives = environment. getlogicaldrives (); Lstenv. Items. Add ("Local disk drive:" + String. Join (",", drives ));
// Obtain all environment variables of the Local Machine Idictionary environmentvariables = environment. getenvironmentvariables (); Foreach (dictionaryentry de in environmentvariables) { Lstenv. Items. Add (De. Key + "=" + De. value ); }
} Public void initregkeydata () { // Obtain information through the Registry Registrykey rkey = registry. localmachine; Rkey = rkey. opensubkey ("Hardware // description // system // centralprocessor // 0 "); Lstregkey. Items. Add ("processor information:" + rkey. getvalue ("processornamestring"). tostring (). Trim ());
Rkey = registry. localmachine; Rkey = rkey. opensubkey ("software // Microsoft // Windows NT // CurrentVersion // networkcards // 1 "); Lstregkey. Items. Add ("Nic information:" + (string) rkey. getvalue ("Description ")); } Public void initapidata () { // 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 ); Lstapi. Items. Add ("Windows path:" + buff. tostring ()); Getsystemdirectory (buff, nchars ); Lstapi. Items. Add ("System Path:" + buff. tostring ()); // Call the getsysteminfo function to obtain CPU Information Cpu_info cpuinfo; Cpuinfo = new cpu_info (); Getsysteminfo (ref cpuinfo ); // The read of CPU information is incorrect. I only have one CPU and read two Lstapi. Items. Add ("This computer has" + cpuinfo. dwnumberofprocessors. tostring () + "CPU "); Lstapi. Items. Add ("CPU type" + cpuinfo. dwprocessortype. tostring ()); Lstapi. Items. Add ("CPU level" + cpuinfo. dwprocessorlevel. tostring ()); Lstapi. Items. Add ("cpu oem id is" + cpuinfo. dwoemid. tostring ()); Lstapi. Items. Add ("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 ); Lstapi. Items. Add (meminfo. dwmemoryload. tostring () + "% memory in use "); Lstapi. Items. Add ("physical memory in total" + meminfo. dwtotalphys. tostring () + "Byte "); Lstapi. Items. Add ("available physical memory" + meminfo. dwavailphys. tostring () + "Byte "); Lstapi. Items. Add ("the total size of the swap file is" + meminfo. dwtotalpagefile. tostring () + "Byte "); Lstapi. Items. Add ("the file size is still interchangeable" + meminfo. dwavailpagefile. tostring () + "Byte "); Lstapi. Items. Add ("Total virtual memory available" + meminfo. dwtotalvirtual. tostring () + "Byte "); Lstapi. Items. Add ("unused virtual memory available" + meminfo. dwavailvirtual. tostring () + "Byte "); // Call the getsystemtime function to obtain the system time information. Systemtime_info stinfo; Stinfo = new systemtime_info (); Getsystemtime (ref stinfo ); Lstapi. items. add (stinfo. wyear. tostring () + "year" + stinfo. wmonth. tostring () + "month" + stinfo. wday. tostring () + "day "); Lstapi. items. add (stinfo. whour + 8 ). tostring () + "point" + stinfo. wminute. tostring () + "Minute" + stinfo. wsecond. tostring () + "seconds "); } |