C#程序设计获取系统信息的Windows窗体主要向你介绍了使用从C#实现关机程序时的界面相关,那么具体的是什麽过程呢?本文就向你介绍相关的内容。
C#程序规划获取体系信息的Windows窗体的布景许多软件都有主动关机功用,特别是在长期下载的时分,这个功用可是使你不用以守候在计算机前面,而电脑却能依照您事前的设定主动封闭。现在咱们用visual C#来编写一个多功用的关机程序。C#程序规划获取体系信息的Windows窗体是该程序的一部分,现在让咱们来看看详细的进程。
C#程序规划获取体系信息的Windows窗体完成的过程:
C#程序规划获取体系信息的Windows窗体完成1. 界面的规划
向工程中增加一个Windows窗体并向窗体中增加如下控件:
C#程序规划获取体系信息的Windows窗体完成2. 在窗体类中引证API函数
- usingSystem.Runtime.InteropServices;
- usingSystem.Text;
- [DllImport("kernel32")]
- publicstaticexternvoidGetWindowsDirectory(StringBuilderWinDir,intcount);
- [DllImport("kernel32")]
- publicstaticexternvoidGetSystemDirectory(StringBuilderSysDir,intcount);
- [DllImport("kernel32")]
- publicstaticexternvoidGetSystemInfo(refCPU_INFOcpuinfo);
- [DllImport("kernel32")]
- publicstaticexternvoidGlobalMemoryStatus(refMEMORY_INFOmeminfo);
- [DllImport("kernel32")]
- publicstaticexternvoidGetSystemTime(refSYSTEMTIME_INFOstinfo);
以上几个API的效果别离是获取体系途径,获得CPU相关信息,获得内存的相关信息,获得体系时刻等。
C#程序规划获取体系信息的Windows窗体完成3. 界说以下各结构
在声明完一切的API函数后,咱们发现后三个函数别离用到了CPU_INFO、MEMORY_INFO、SYSTEMTIME_INFO等结构,这些结构并非是.Net内部的,它们从何而来?其实,咱们在用到以上API调用时均需用到以上结构,咱们将函数调用获得的信息存放在以上的结构体中,***回来给程序输出。这些结构体比较复杂,可是假如开发者可以娴熟运用,那么整个API国际将尽在开发者的把握之中。以下便是上述结构体的声明:
- //界说CPU的信息结构
- [StructLayout(LayoutKind.Sequential)]
- publicstructCPU_INFO
- {
- publicuintdwOemId;
- publicuintdwPageSize;
- publicuintlpMinimumApplicationAddress;
- publicuintlpMaximumApplicationAddress;
- publicuintdwActiveProcessorMask;
- publicuintdwNumberOfProcessors;
- publicuintdwProcessorType;
- publicuintdwAllocationGranularity;
- publicuintdwProcessorLevel;
- publicuintdwProcessorRevision;
- }
- file://界说内存的信息结构
- [StructLayout(LayoutKind.Sequential)]
- publicstructMEMORY_INFO
- {
- publicuintdwLength;
- publicuintdwMemoryLoad;
- publicuintdwTotalPhys;
- publicuintdwAvailPhys;
- publicuintdwTotalPageFile;
- publicuintdwAvailPageFile;
- publicuintdwTotalVirtual;
- publicuintdwAvailVirtual;
- }
- file://界说体系时刻的信息结构
- [StructLayout(LayoutKind.Sequential)]
- publicstructSYSTEMTIME_INFO
- {
- publicushortwYear;
- publicushortwMonth;
- publicushortwDayOfWeek;
- publicushortwDay;
- publicushortwHour;
- publicushortwMinute;
- publicushortwSecond;
- publicushortwMilliseconds;
- }
C#程序规划获取体系信息的Windows窗体完成4. 编写窗体类的办法
- privatevoidbutton1_Click(objectsender,System.EventArgse)
- {
- file://调用GetWindowsDirectory和GetSystemDirectory函数别离获得Windows途径和体系途径
- constintnChars=128;
- StringBuilderBuff=newStringBuilder(nChars);
- GetWindowsDirectory(Buff,nChars);
- WindowsDirectory.Text="Windows途径:"+Buff.ToString();
- GetSystemDirectory(Buff,nChars);
- SystemDirectory.Text="体系途径:"+Buff.ToString();
- file://调用GetSystemInfo函数获取CPU的相关信息
- CPU_INFOCpuInfo;
- CpuInfo=newCPU_INFO();
- GetSystemInfo(refCpuInfo);
- NumberOfProcessors.Text=
- "本计算机中有"+CpuInfo.dwNumberOfProcessors.ToString()+"个CPU";
- ProcessorType.Text="CPU的类型为"+CpuInfo.dwProcessorType.ToString();
- ProcessorLevel.Text="CPU等级为"+CpuInfo.dwProcessorLevel.ToString();
- OemId.Text="CPU的OEMID为"+CpuInfo.dwOemId.ToString();
- PageSize.Text="CPU中的页面巨细为"+CpuInfo.dwPageSize.ToString();
- file://调用GlobalMemoryStatus函数获取内存的相关信息
- MEMORY_INFOMemInfo;
- MemInfo=newMEMORY_INFO();
- GlobalMemoryStatus(refMemInfo);
- MemoryLoad.Text=
- MemInfo.dwMemoryLoad.ToString()+"%的内存正在运用";
- TotalPhys.Text=
- "物理内存共有"+MemInfo.dwTotalPhys.ToString()+"字节";
- AvailPhys.Text=
- "可运用的物理内存有"+MemInfo.dwAvailPhys.ToString()+"字节";
- TotalPageFile.Text=
- "交流文件总巨细为"+MemInfo.dwTotalPageFile.ToString()+"字节";
- AvailPageFile.Text=
- "尚可交流文件巨细为"+MemInfo.dwAvailPageFile.ToString()+"字节";
- TotalVirtual.Text=
- "总虚拟内存有"+MemInfo.dwTotalVirtual.ToString()+"字节";
- AvailVirtual.Text=
- "未用虚拟内存有"+MemInfo.dwAvailVirtual.ToString()+"字节";
- file://调用GetSystemTime函数获取体系时刻信息
- SYSTEMTIME_INFOStInfo;
- StInfo=newSYSTEMTIME_INFO();
- GetSystemTime(refStInfo);
- Date.Text=StInfo.wYear.ToString()+
- "年"+StInfo.wMonth.ToString()+"月"+
- StInfo.wDay.ToString()+"日";
- Time.Text=(StInfo.wHour+8).ToString()+
- "点"+StInfo.wMinute.ToString()+"分"+
- StInfo.wSecond.ToString()+"秒";
- }
C#程序规划获取体系信息的Windows窗体完成的基本情况就向你介绍到这儿,期望对你了解和学习C#程序规划获取体系信息的Windows窗体完成有所协助。
【修改引荐】
- 怎么初始化数组详解
- C#数组操作的领会浅谈
- C#关机代码实例详解
- C#关机代码的完成浅析
- C#程序规划封闭Windows窗体浅析