Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the Windows form of system information in C #

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "Windows form of how to get system information in C#", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn the Windows form of how to get system information in C#.

Background of Windows form for C # programming to obtain system information many software have automatic shutdown function, especially when downloading for a long time, this function allows you not to wait in front of the computer, while the computer can shut down automatically according to your prior settings. Now we use visual C # to write a multi-functional shutdown program. C # programming Windows forms to get system information is part of the program, now let's take a look at the specific process.

C # programming steps to implement Windows forms to obtain system information:

C # programming to obtain system information of the Windows form implementation 1. Interface design

Add a Windows form to the project and add the following controls to the form:

C # programming to obtain the system information of the Windows form implementation 2. Reference the API function in the form class

Using System.Runtime.InteropServices; using System.Text; [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)

The functions of the above API are to obtain the system path, CPU-related information, memory-related information, system time and so on.

C # programming to obtain system information of the Windows form implementation 3. Define the following structures

After declaring all the API functions, we find that the last three functions use CPU_INFO, MEMORY_INFO, SYSTEMTIME_INFO and other structures, which are not internal to .net. Where do they come from? In fact, we need to use the above structure when we use the above API calls. We store the information obtained by the function calls in the above structure and return it to the program output. These structures are complex, but if developers can use them skillfully, then the whole API world will be in the hands of developers. The following is the declaration of the above structure:

/ define the information structure of CPU [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;} file:// define the information structure of memory [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;} file:// defines the information structure of system time [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;}

C # programming to obtain system information Windows form implementation 4. The method of writing a form class

Private void button1_Click (object sender, System.EventArgs e) {file:// calls GetWindowsDirectory and GetSystemDirectory functions to get Windows path and system path const int nChars = 128,StringBuilder Buff = new StringBuilder (nChars); GetWindowsDirectory (Buff,nChars); WindowsDirectory.Text = "Windows path:" + Buff.ToString (); GetSystemDirectory (Buff,nChars); SystemDirectory.Text = "system path:" + Buff.ToString () File:// calls the GetSystemInfo function to get information about CPU CPU_INFO CpuInfo; CpuInfo = new CPU_INFO (); GetSystemInfo (ref CpuInfo); NumberOfProcessors.Text = "there are + CpuInfo.dwNumberOfProcessors.ToString () +" CPU "in this computer; ProcessorType.Text =" CPU type is "+ CpuInfo.dwProcessorType.ToString (); ProcessorLevel.Text =" CPU level "+ CpuInfo.dwProcessorLevel.ToString () OemId.Text = "OEM ID of CPU is" + CpuInfo.dwOemId.ToString (); PageSize.Text = "Page size in CPU is" + CpuInfo.dwPageSize.ToString (); file:// calls GlobalMemoryStatus function to get information about memory MEMORY_INFO MemInfo; MemInfo = new MEMORY_INFO (); GlobalMemoryStatus (ref MemInfo); MemoryLoad.Text = MemInfo.dwMemoryLoad.ToString () + "% of memory is in use" TotalPhys.Text = "physical memory sharing" + MemInfo.dwTotalPhys.ToString () + "bytes"; AvailPhys.Text = "usable physical memory" + MemInfo.dwAvailPhys.ToString () + "bytes"; TotalPageFile.Text = "Total swap file size" + MemInfo.dwTotalPageFile.ToString () + "bytes"; AvailPageFile.Text = "still exchangeable file size" + MemInfo.dwAvailPageFile.ToString () + "bytes" TotalVirtual.Text = "total virtual memory" + MemInfo.dwTotalVirtual.ToString () + "bytes"; AvailVirtual.Text = "unused virtual memory" + MemInfo.dwAvailVirtual.ToString () + "bytes"; file:// calls GetSystemTime function to get 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 () + minutes + StInfo.wSecond.ToString () + seconds } these are all the contents of the article "how to get system Information in Windows form in C #". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report