5.編寫窗體類的方法
private void button1_Click(object sender, System.EventArgs e )
{
file://調用GetWindowsDirectory和GetSystemDirectory函數分別取得Windows路徑和系統路徑
const int nChars = 128 ;
StringBuilder Buff = new StringBuilder(nChars) ;
GetWindowsDirectory(Buff,nChars) ;
WindowsDirectory.Text = "Windows路徑:"+Buff.ToString( ) ;
GetSystemDirectory(Buff,nChars) ;
SystemDirectory.Text = " 系統路徑:"+Buff.ToString( ) ;
file://調用GetSystemInfo函數獲取CPU的相關信息
CPU_INFO CpuInfo ;
CpuInfo = new CPU_INFO( ) ;
GetSystemInfo(ref CpuInfo) ;
NumberOfProcessors.Text = "本計算機中有"+CpuInfo.dwNumberOfProcessors.ToString( ) +"個CPU";
ProcessorType.Text = "CPU的類型為"+CpuInfo.dwProcessorType.ToString( ) ;
ProcessorLevel.Text = "CPU等級為"+CpuInfo.dwProcessorLevel.ToString( ) ;
OemId.Text = "CPU的OEM ID為"+CpuInfo.dwOemId.ToString( ) ;
PageSize.Text = "CPU中的頁面大小為"+CpuInfo.dwPageSize.ToString( ) ;
file://調用GlobalMemoryStatus函數獲取內存的相關信息
MEMORY_INFO MemInfo ;
MemInfo = new MEMORY_INFO( ) ;
GlobalMemoryStatus(ref MemInfo) ;
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_INFO StInfo ;
StInfo = new SYSTEMTIME_INFO( ) ;
GetSystemTime(ref StInfo) ;
Date.Text = StInfo.wYear.ToString( ) +"年"+StInfo.wMonth.ToString( ) +"月"+StInfo.wDay.ToString( ) +"日" ;
Time.Text = (StInfo.wHour+8).ToString( ) +"點"+StInfo.wMinute.ToString( ) +"分"+StInfo.wSecond.ToString( ) +"秒" ;
}
三.結束語。
上面介紹了Visual C#開發多功能關機程序的整個過程,該程序有一定的實用價值。通過本文的學習,我相信稍有API使用基礎的開發者可以馬上觸類旁通,很快掌握Visual C#中對API的操作。上面給出的實例僅僅是一個簡單的程序,不過有興趣的讀者可以進一步完善其功能,做出更完美的系統應用程序。