C # obtain the hard disk number, CPU number and type, memory size, and usage

Source: Internet
Author: User
If C # is used to obtain the environment and attributes of the system, many questions are asked online, but most of the questions are not answered. Recently, I just wanted to do something about it, I have sorted it out and provided it to you. I hope to give you some reference: first, I need to define several structures (struct) to facilitate dllimport to be called as the return parameter. The following code is cpuinfo. CS using system; using system. configuration; using system. runtime. interopservices;/** // *** layoutkind. automatic: to improve efficiency, allow the running state to reorder type members * Note: never use this option to call uncontrolled Dynamic Linked Library functions. * Layoutkind. explicit: sorts the type members of each domain by fieldoffset attribute * layoutkind. Sequential: sorts the type members in the memory that are not under the jurisdiction of the type definition. * // ** // <Summary> // defines the CPU information structure. // </Summary> [structlayout (layoutkind. sequential)] public struct cpuinfo... {/** // <summary> // oem id /// </Summary> Public uint dwoemid; /** // <summary> // page size // </Summary> Public uint dwpagesize; Public uint lpminimumapplicationaddress; Public uint lpmaximumapplicationaddress; Public uint dwactiveprocessormask; /** // <summary> // Number of CPUs /// </su Mmary> Public uint dwnumberofprocessors;/** // <summary> // CPU type // </Summary> Public uint dwprocessortype; Public uint dwallocationgranularity; /** // <summary> // CPU level // </Summary> Public uint dwprocessorlevel; Public uint dwprocessorrevision;} memoryinfo. CS using system; using system. configuration; using system. runtime. interopservices;/** // *** layoutkind. automatic New sorting * Note: never use this option to call uncontrolled dynamic link library functions. * Layoutkind. explicit: sorts the type members of each domain by fieldoffset attribute * layoutkind. Sequential: sorts the type members in the memory that are not under the jurisdiction of the type definition. * // ** // <Summary> // defines the memory information structure. // </Summary> [structlayout (layoutkind. sequential)] public struct memoryinfo... {/*** // <summary> ///// </Summary> Public uint dwlength; /** //// <summary> /// used memory /// </Summary> Public uint dwmemoryload; /** //// <summary> /// total physical memory size // </Summary> Public uint dwtotalphys; /** // <summary> // available physical memory size // </Summary> Public uint dwavailphys; /** //// <summary> /// the total size of the swap file /// </Summary> Public uint dwtotalpagefile; /** //// <summary> // available swap file size // </Summary> Public uint dwavailpagefile; /** //// <summary> /// Total virtual memory size // </Summary> Public uint dwtotalvirtual; /** // <summary> // available virtual memory size // </Summary> Public uint dwavailvirtual ;}
 
Systemtimeinfo. CS using system; using system. configuration; using system. runtime. interopservices;/** // *** layoutkind. automatic: to improve efficiency, allow the running state to reorder type members * Note: never use this option to call uncontrolled Dynamic Linked Library functions. * Layoutkind. explicit: sorts the type members of each domain by fieldoffset attribute * layoutkind. Sequential: sorts the type members in the memory that are not under the jurisdiction of the type definition. * // ** // <Summary> // defines the information structure of the system time. // </Summary> [structlayout (layoutkind. sequential)] public struct systemtimeinfo... {/** // <summary> // year // </Summary> Public ushort wyear; /** // <summary> // month // </Summary> Public ushort wmonth; /** //// <summary> // week // </Summary> Public ushort wdayofweek; /** // <summary> // day // </Summary> Public ushort wday; /** // <summary> // hour // </Summary> Public ushort whour; /** // <summary> /// minute // </Summary> Public ushort wminute; /** // <summary> // second // </Summary> Public ushort wsecond; /** // <summary> // millisecond // </Summary> Public ushort wmilliseconds;} A call class systeminfo is also defined. CS, the Code is as follows: using system; using system. configuration; using system. runtime. interopservices; using system. management; using system. text;/** // <summary> // Summary of systeminfo /// </Summary> public class systeminfo... {private const int char_count = 128; Public systeminfo ()... {} [dllimport ("Kernel32")] Private Static extern void getwindowsdirectory (stringbuilder WINDIR, int count); [dllimport ("Kernel32")] Private Static extern void getsystemdirectory (stringbuilder Sysdir, int count); [dllimport ("Kernel32")] Private Static extern void getsysteminfo (ref cpuinfo); [dllimport ("Kernel32")] private Static extern void globalmemorystatus (ref memoryinfo meminfo); [dllimport ("Kernel32")] Private Static extern void getsystemtime (ref systemtimeinfo sysinfo ); /*** //// <summary> /// query the cpu id /// </Summary> /// <returns> </returns> Public String getcpuid ()... {managementclass mclass = new managementclass ("win32_processor"); managementobjectcollection MOC = mclass. getinstances (); string cpuid = NULL; foreach (managementobject Mo in MoC )... {cpuid = Mo. properties ["processorid"]. value. tostring (); break;} return cpuid ;} /*** //// <summary> /// query the hard disk number /// </Summary> /// <returns> </returns> Public String getmainharddiskid ()... {managementobjectsearcher searcher = new managementobjectsearcher ("select * From win32_physicalmedia"); string harddiskid = NULL; foreach (managementobject Mo in searcher. get ())... {harddiskid = Mo ["serialnumber"]. tostring (). trim (); break;} return harddiskid ;} /*** // <summary> // obtain the Windows directory /// </Summary> /// <returns> </returns> Public String getwindirectory ()... {stringbuilder sbuilder = new stringbuilder (char_count); getwindowsdirectory (sbuilder, char_count); Return sbuilder. tostring ();} /*** // <summary> // obtain the system directory /// </Summary> /// <returns> </returns> Public String getsysdirectory ()... {stringbuilder sbuilder = new stringbuilder (char_count); getsystemdirectory (sbuilder, char_count); Return sbuilder. tostring ();} /*** // <summary> // obtain CPU information /// </Summary> /// <returns> </returns> Public cpuinfo getcpuinfo ()... {cpuinfo = new cpuinfo (); getsysteminfo (ref cpuinfo); Return cpuinfo ;} /*** // <summary> // obtain the system memory information /// </Summary> /// <returns> </returns> Public memoryinfo getmemoryinfo (). .. {memoryinfo = new memoryinfo (); globalmemorystatus (ref memoryinfo); Return memoryinfo ;} /*** // <summary> // obtain the system time information /// </Summary> /// <returns> </returns> Public systemtimeinfo getsystemtimeinfo (). .. {systemtimeinfo = new systemtimeinfo (); getsystemtime (ref systemtimeinfo); Return systemtimeinfo ;} /*** // <summary> // obtain the system name /// </Summary> /// <returns> </returns> Public String getoperationsysteminname ()... {operatingsystem OS = system. environment. osversion; string osname = "unknown"; Switch (OS. platform )... {Case platformid. win32windows: Switch (OS. version. minor )... {Case 0: osname = "Windows 95"; break; case 10: osname = "Windows 98"; break; Case 90: osname = "Windows ME"; break;} break; case platformid. win32nt: Switch (OS. version. major )... {Case 3: osname = "windws NT 3.51"; break; Case 4: osname = "Windows NT 4"; break; Case 5: If (OS. version. minor = 0 )... {osname = "Windows 2000";} else if (OS. version. minor = 1 )... {osname = "Windows XP";} else if (OS. version. minor = 2 )... {osname = "Windows Server 2003";} break; Case 6: osname = "Longhorn"; break;} return string. format ("{0}, {1}", osname, OS. version. tostring ());}}
Top
 
The following example is called. For simplicity, I output the code on An ASPX page. However, this program can be called in winform: using system; using system. data; using system. configuration; using system. collections; using system. collections. specialized; 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 system. runtime. interopservices; public partial Class index: system. Web. UI. Page... {protected void page_load (Object sender, eventargs e)... {If (! Page. ispostback )... {systeminfo = new systeminfo (); response. write ("Operating System:" + systeminfo. getoperationsysteminname () + "<br>"); response. write ("CPU No.:" + systeminfo. getcpuid () + "<br>"); response. write ("Hard Disk number:" + systeminfo. getmainharddiskid () + "<br>"); response. write ("Windows directory location:" + systeminfo. getsysdirectory () + "<br>"); response. write ("system directory location:" + systeminfo. getwindirectory () + "<br> "); Memoryinfo = systeminfo. getmemoryinfo (); cpuinfo = systeminfo. getcpuinfo (); response. write ("dwactiveprocessormask" + cpuinfo. dwactiveprocessormask + "<br>"); response. write ("dwallocationgranularity" + cpuinfo. dwallocationgranularity + "<br>"); response. write ("CPU count:" + cpuinfo. dwnumberofprocessors + "<br>"); response. write ("oem id:" + cpuinfo. dwoemid + "<br>"); response. write ("Page size" + cpuinfo. dwpagesize + "<br>"); response. write ("CPU level" + cpuinfo. dwprocessorlevel + "<br>"); response. write ("dwprocessorrevision" + cpuinfo. dwprocessorrevision + "<br>"); response. write ("CPU type" + cpuinfo. dwprocessortype + "<br>"); response. write ("lpmaximumapplicationaddress" + cpuinfo. lpmaximumapplicationaddress + "<br>"); response. write ("lpminimumapplicationaddress" + cpuinfo. lpminimumappl Icationaddress + "<br>"); response. write ("CPU type:" + cpuinfo. dwprocessortype + "<br>"); response. write ("available swap file size:" + memoryinfo. dwavailpagefile + "<br>"); response. write ("available physical memory size:" + memoryinfo. dwavailphys + "<br>"); response. write ("available virtual memory size" + memoryinfo. dwavailvirtual + "<br>"); response. write ("OS bits:" + memoryinfo. dwlength + "<br>"); response. write ("memory used:" + memoryinfo. dwmemoryload + "<br> "); Response. write ("Total swap file size:" + memoryinfo. dwtotalpagefile + "<br>"); response. write ("total physical memory size:" + memoryinfo. dwtotalphys + "<br>"); response. write ("Total virtual memory size:" + memoryinfo. dwtotalvirtual + "<br>") ;}}note: the front-end ASPX page does not have any controls. ------------------------------------------ Complete content see: http://blog.csdn.net/zhoufoxcn/archive/2007/03/20/1534949.aspx

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.