C#程序設(shè)計(jì)獲取系統(tǒng)信息的Windows窗體淺析
C#程序設(shè)計(jì)獲取系統(tǒng)信息的Windows窗體的背景許多軟件都有自動(dòng)關(guān)機(jī)功能,特別是在長時(shí)間下載的時(shí)候,這個(gè)功能可是使你不用以守候在計(jì)算機(jī)前面,而電腦卻能按照您事先的設(shè)定自動(dòng)關(guān)閉。現(xiàn)在我們用visual C#來編寫一個(gè)多功能的關(guān)機(jī)程序。C#程序設(shè)計(jì)獲取系統(tǒng)信息的Windows窗體是該程序的一部分,現(xiàn)在讓我們來看看具體的過程。
C#程序設(shè)計(jì)獲取系統(tǒng)信息的Windows窗體實(shí)現(xiàn)的步驟:
C#程序設(shè)計(jì)獲取系統(tǒng)信息的Windows窗體實(shí)現(xiàn)1. 界面的設(shè)計(jì)
向工程中增加一個(gè)Windows窗體并向窗體中添加如下控件:
 
C#程序設(shè)計(jì)獲取系統(tǒng)信息的Windows窗體實(shí)現(xiàn)2. 在窗體類中引用API函數(shù)
- 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) ;
 
以上幾個(gè)API的作用分別是獲取系統(tǒng)路徑,獲得CPU相關(guān)信息,獲得內(nèi)存的相關(guān)信息,獲得系統(tǒng)時(shí)間等。
C#程序設(shè)計(jì)獲取系統(tǒng)信息的Windows窗體實(shí)現(xiàn)3. 定義以下各結(jié)構(gòu)
在聲明完所有的API函數(shù)后,我們發(fā)現(xiàn)后三個(gè)函數(shù)分別用到了CPU_INFO、MEMORY_INFO、SYSTEMTIME_INFO等結(jié)構(gòu),這些結(jié)構(gòu)并非是.Net內(nèi)部的,它們從何而來?其實(shí),我們?cè)谟玫揭陨螦PI調(diào)用時(shí)均需用到以上結(jié)構(gòu),我們將函數(shù)調(diào)用獲得的信息存放在以上的結(jié)構(gòu)體中,***返回給程序輸出。這些結(jié)構(gòu)體比較復(fù)雜,但是如果開發(fā)者能夠熟練運(yùn)用,那么整個(gè)API世界將盡在開發(fā)者的掌握之中。以下就是上述結(jié)構(gòu)體的聲明:
- //定義CPU的信息結(jié)構(gòu)
 - [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://定義內(nèi)存的信息結(jié)構(gòu)
 - [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://定義系統(tǒng)時(shí)間的信息結(jié)構(gòu)
 - [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#程序設(shè)計(jì)獲取系統(tǒng)信息的Windows窗體實(shí)現(xiàn)4. 編寫窗體類的方法
- private void button1_Click(object sender, System.EventArgs e )
 - {
 - file://調(diào)用GetWindowsDirectory和GetSystemDirectory函數(shù)分別取得Windows路徑和系統(tǒng)路徑
 - const int nChars = 128 ;
 - StringBuilder Buff = new StringBuilder(nChars) ;
 - GetWindowsDirectory(Buff,nChars) ;
 - WindowsDirectory.Text = "Windows路徑:"+Buff.ToString( ) ;
 - GetSystemDirectory(Buff,nChars) ;
 - SystemDirectory.Text = " 系統(tǒng)路徑:"+Buff.ToString( ) ;
 - file://調(diào)用GetSystemInfo函數(shù)獲取CPU的相關(guān)信息
 - CPU_INFO CpuInfo ;
 - CpuInfo = new CPU_INFO( ) ;
 - GetSystemInfo(ref CpuInfo) ;
 - NumberOfProcessors.Text =
 - "本計(jì)算機(jī)中有"+CpuInfo.dwNumberOfProcessors.ToString( ) +"個(gè)CPU";
 - ProcessorType.Text = "CPU的類型為"+CpuInfo.dwProcessorType.ToString( ) ;
 - ProcessorLevel.Text = "CPU等級(jí)為"+CpuInfo.dwProcessorLevel.ToString( ) ;
 - OemId.Text = "CPU的OEM ID為"+CpuInfo.dwOemId.ToString( ) ;
 - PageSize.Text = "CPU中的頁面大小為"+CpuInfo.dwPageSize.ToString( ) ;
 - file://調(diào)用GlobalMemoryStatus函數(shù)獲取內(nèi)存的相關(guān)信息
 - MEMORY_INFO MemInfo ;
 - MemInfo = new MEMORY_INFO( ) ;
 - GlobalMemoryStatus(ref MemInfo) ;
 - MemoryLoad.Text =
 - MemInfo.dwMemoryLoad.ToString( ) +"%的內(nèi)存正在使用" ;
 - TotalPhys.Text =
 - "物理內(nèi)存共有"+MemInfo.dwTotalPhys.ToString( ) +"字節(jié)" ;
 - AvailPhys.Text =
 - "可使用的物理內(nèi)存有"+MemInfo.dwAvailPhys.ToString( ) +"字節(jié)" ;
 - TotalPageFile.Text =
 - "交換文件總大小為"+MemInfo.dwTotalPageFile.ToString( ) +"字節(jié)" ;
 - AvailPageFile.Text =
 - "尚可交換文件大小為"+MemInfo.dwAvailPageFile.ToString( ) +"字節(jié)" ;
 - TotalVirtual.Text =
 - "總虛擬內(nèi)存有"+MemInfo.dwTotalVirtual.ToString( ) +"字節(jié)" ;
 - AvailVirtual.Text =
 - "未用虛擬內(nèi)存有"+MemInfo.dwAvailVirtual.ToString( ) +"字節(jié)" ;
 - file://調(diào)用GetSystemTime函數(shù)獲取系統(tǒng)時(shí)間信息
 - 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( ) +
 - "點(diǎn)"+StInfo.wMinute.ToString( ) +"分"+
 - StInfo.wSecond.ToString( ) +"秒" ;
 - }
 
C#程序設(shè)計(jì)獲取系統(tǒng)信息的Windows窗體實(shí)現(xiàn)的基本情況就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#程序設(shè)計(jì)獲取系統(tǒng)信息的Windows窗體實(shí)現(xiàn)有所幫助。
【編輯推薦】















 
 
 
 
 
 
 