自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#程序設(shè)計獲取系統(tǒng)信息的Windows窗體淺析

開發(fā) 后端
C#程序設(shè)計獲取系統(tǒng)信息的Windows窗體主要向你介紹了使用從C#實現(xiàn)關(guān)機程序時的界面相關(guān),那么具體的是什麼過程呢?本文就向你介紹相關(guān)的內(nèi)容。

C#程序設(shè)計獲取系統(tǒng)信息的Windows窗體的背景許多軟件都有自動關(guān)機功能,特別是在長時間下載的時候,這個功能可是使你不用以守候在計算機前面,而電腦卻能按照您事先的設(shè)定自動關(guān)閉?,F(xiàn)在我們用visual C#來編寫一個多功能的關(guān)機程序。C#程序設(shè)計獲取系統(tǒng)信息的Windows窗體是該程序的一部分,現(xiàn)在讓我們來看看具體的過程。

C#程序設(shè)計獲取系統(tǒng)信息的Windows窗體實現(xiàn)的步驟:

C#程序設(shè)計獲取系統(tǒng)信息的Windows窗體實現(xiàn)1. 界面的設(shè)計

向工程中增加一個Windows窗體并向窗體中添加如下控件:

向窗體中添加控件 

C#程序設(shè)計獲取系統(tǒng)信息的Windows窗體實現(xiàn)2. 在窗體類中引用API函數(shù)

  1. using System.Runtime.InteropServices ;   
  2. using System.Text ;   
  3. [ DllImport("kernel32") ]   
  4. public static extern void GetWindowsDirectory(StringBuilder WinDir,int count) ;   
  5. [ DllImport("kernel32") ]   
  6. public static extern void GetSystemDirectory(StringBuilder SysDir,int count) ;   
  7. [ DllImport("kernel32") ]   
  8. public static extern void GetSystemInfo(ref CPU_INFO cpuinfo) ;   
  9. [ DllImport("kernel32") ]   
  10. public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo) ;   
  11. [ DllImport("kernel32") ]   
  12. public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo) ;  

以上幾個API的作用分別是獲取系統(tǒng)路徑,獲得CPU相關(guān)信息,獲得內(nèi)存的相關(guān)信息,獲得系統(tǒng)時間等。

C#程序設(shè)計獲取系統(tǒng)信息的Windows窗體實現(xiàn)3. 定義以下各結(jié)構(gòu)

在聲明完所有的API函數(shù)后,我們發(fā)現(xiàn)后三個函數(shù)分別用到了CPU_INFO、MEMORY_INFO、SYSTEMTIME_INFO等結(jié)構(gòu),這些結(jié)構(gòu)并非是.Net內(nèi)部的,它們從何而來?其實,我們在用到以上API調(diào)用時均需用到以上結(jié)構(gòu),我們將函數(shù)調(diào)用獲得的信息存放在以上的結(jié)構(gòu)體中,***返回給程序輸出。這些結(jié)構(gòu)體比較復(fù)雜,但是如果開發(fā)者能夠熟練運用,那么整個API世界將盡在開發(fā)者的掌握之中。以下就是上述結(jié)構(gòu)體的聲明:

  1. //定義CPU的信息結(jié)構(gòu)   
  2. [StructLayout(LayoutKind.Sequential) ]   
  3. public struct CPU_INFO   
  4. {   
  5. public uint dwOemId ;   
  6. public uint dwPageSize ;   
  7. public uint lpMinimumApplicationAddress ;   
  8. public uint lpMaximumApplicationAddress ;   
  9. public uint dwActiveProcessorMask ;   
  10. public uint dwNumberOfProcessors ;   
  11. public uint dwProcessorType ;   
  12. public uint dwAllocationGranularity ;   
  13. public uint dwProcessorLevel ;   
  14. public uint dwProcessorRevision ;   
  15. }   
  16.  
  17. file://定義內(nèi)存的信息結(jié)構(gòu)   
  18. [StructLayout(LayoutKind.Sequential) ]   
  19. public struct MEMORY_INFO   
  20. {   
  21. public uint dwLength ;   
  22. public uint dwMemoryLoad ;   
  23. public uint dwTotalPhys ;   
  24. public uint dwAvailPhys ;   
  25. public uint dwTotalPageFile ;   
  26. public uint dwAvailPageFile ;   
  27. public uint dwTotalVirtual ;   
  28. public uint dwAvailVirtual ;   
  29. }   
  30.  
  31. file://定義系統(tǒng)時間的信息結(jié)構(gòu)   
  32. [StructLayout(LayoutKind.Sequential) ]   
  33. public struct SYSTEMTIME_INFO   
  34. {   
  35. public ushort wYear ;   
  36. public ushort wMonth ;   
  37. public ushort wDayOfWeek ;   
  38. public ushort wDay ;   
  39. public ushort wHour ;   
  40. public ushort wMinute ;   
  41. public ushort wSecond ;   
  42. public ushort wMilliseconds ;   
  43. }   

C#程序設(shè)計獲取系統(tǒng)信息的Windows窗體實現(xiàn)4. 編寫窗體類的方法

  1. private void button1_Click(object sender, System.EventArgs e )   
  2. {   
  3. file://調(diào)用GetWindowsDirectory和GetSystemDirectory函數(shù)分別取得Windows路徑和系統(tǒng)路徑   
  4. const int nChars = 128 ;   
  5. StringBuilder Buff = new StringBuilder(nChars) ;   
  6. GetWindowsDirectory(Buff,nChars) ;   
  7. WindowsDirectory.Text = "Windows路徑:"+Buff.ToString( ) ;   
  8. GetSystemDirectory(Buff,nChars) ;   
  9. SystemDirectory.Text = " 系統(tǒng)路徑:"+Buff.ToString( ) ;   
  10.  
  11. file://調(diào)用GetSystemInfo函數(shù)獲取CPU的相關(guān)信息   
  12. CPU_INFO CpuInfo ;   
  13. CpuInfo = new CPU_INFO( ) ;   
  14. GetSystemInfo(ref CpuInfo) ;   
  15. NumberOfProcessors.Text =   
  16. "本計算機中有"+CpuInfo.dwNumberOfProcessors.ToString( ) +"個CPU";   
  17. ProcessorType.Text = "CPU的類型為"+CpuInfo.dwProcessorType.ToString( ) ;   
  18. ProcessorLevel.Text = "CPU等級為"+CpuInfo.dwProcessorLevel.ToString( ) ;   
  19. OemId.Text = "CPU的OEM ID為"+CpuInfo.dwOemId.ToString( ) ;   
  20. PageSize.Text = "CPU中的頁面大小為"+CpuInfo.dwPageSize.ToString( ) ;   
  21.  
  22. file://調(diào)用GlobalMemoryStatus函數(shù)獲取內(nèi)存的相關(guān)信息   
  23. MEMORY_INFO MemInfo ;   
  24. MemInfo = new MEMORY_INFO( ) ;   
  25. GlobalMemoryStatus(ref MemInfo) ;   
  26. MemoryLoad.Text =   
  27. MemInfo.dwMemoryLoad.ToString( ) +"%的內(nèi)存正在使用" ;   
  28. TotalPhys.Text =   
  29. "物理內(nèi)存共有"+MemInfo.dwTotalPhys.ToString( ) +"字節(jié)" ;   
  30. AvailPhys.Text =   
  31. "可使用的物理內(nèi)存有"+MemInfo.dwAvailPhys.ToString( ) +"字節(jié)" ;   
  32. TotalPageFile.Text =   
  33. "交換文件總大小為"+MemInfo.dwTotalPageFile.ToString( ) +"字節(jié)" ;   
  34. AvailPageFile.Text =   
  35. "尚可交換文件大小為"+MemInfo.dwAvailPageFile.ToString( ) +"字節(jié)" ;   
  36. TotalVirtual.Text =   
  37. "總虛擬內(nèi)存有"+MemInfo.dwTotalVirtual.ToString( ) +"字節(jié)" ;   
  38. AvailVirtual.Text =   
  39. "未用虛擬內(nèi)存有"+MemInfo.dwAvailVirtual.ToString( ) +"字節(jié)" ;   
  40.  
  41. file://調(diào)用GetSystemTime函數(shù)獲取系統(tǒng)時間信息   
  42. SYSTEMTIME_INFO StInfo ;   
  43. StInfo = new SYSTEMTIME_INFO( ) ;   
  44. GetSystemTime(ref StInfo) ;   
  45. Date.Text = StInfo.wYear.ToString( ) +  
  46. "年"+StInfo.wMonth.ToString( ) +"月"+  
  47. StInfo.wDay.ToString( ) +"日" ;   
  48. Time.Text = (StInfo.wHour+8).ToString( ) +  
  49. "點"+StInfo.wMinute.ToString( ) +"分"+  
  50. StInfo.wSecond.ToString( ) +"秒" ;   
  51. }   

C#程序設(shè)計獲取系統(tǒng)信息的Windows窗體實現(xiàn)的基本情況就向你介紹到這里,希望對你了解和學(xué)習(xí)C#程序設(shè)計獲取系統(tǒng)信息的Windows窗體實現(xiàn)有所幫助。

【編輯推薦】

  1. 如何初始化數(shù)組詳解
  2. C#數(shù)組操作的體會淺談
  3. C#關(guān)機代碼實例詳解
  4. C#關(guān)機代碼的實現(xiàn)淺析
  5. C#程序設(shè)計關(guān)閉Windows窗體淺析
責(zé)任編輯:仲衡 來源: it.21cn.com
相關(guān)推薦

2009-09-02 17:28:26

C#程序設(shè)計Windows窗體

2009-08-14 16:41:22

C#啟動Windows

2009-09-07 06:07:46

C#窗體設(shè)計

2009-08-25 09:39:21

創(chuàng)建C# Window

2009-08-20 10:10:55

C#透明窗體

2009-08-21 17:48:43

C#網(wǎng)絡(luò)編程

2010-01-11 10:34:22

C++程序

2009-09-07 04:19:56

C#窗體事件

2009-08-14 11:00:16

C#創(chuàng)建Windows

2024-07-09 17:09:49

C#開發(fā)Windows窗體

2009-08-21 17:33:34

服務(wù)器端程序C#網(wǎng)絡(luò)編程

2009-09-07 04:56:52

C#模式窗體

2009-09-07 05:31:39

C#窗體關(guān)閉事件

2009-09-07 03:37:51

C#窗體

2009-09-02 13:22:23

C#組件化程序設(shè)計

2009-09-07 06:56:46

C#透明窗體

2009-09-07 05:24:22

C#窗體繼承

2012-03-14 10:48:05

C#

2010-01-11 17:22:02

2009-08-14 16:02:50

C#啟動windows
點贊
收藏

51CTO技術(shù)棧公眾號