程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C#程序員經常用到的10個實用代碼片段(1)

C#程序員經常用到的10個實用代碼片段(1)

編輯:C++入門知識

C#程序員經常用到的10個實用代碼片段(1)


1 讀取操作系統和CLR的版本

  1. OperatingSystem os = System.Environment.OSVersion; 
  2. Console.WriteLine(“Platform: {0}”, os.Platform); 
  3. Console.WriteLine(“Service Pack: {0}”, os.ServicePack); 
  4. Console.WriteLine(“Version: {0}”, os.Version); 
  5. Console.WriteLine(“VersionString: {0}”, os.VersionString); 
  6. Console.WriteLine(“CLR Version: {0}”, System.Environment.Version); 

在我的Windows 7系統中,輸出以下信息

  1. Platform: Win32NT 
  2. Service Pack: 
  3. Version: 6.1.7600.0 
  4. VersionString: Microsoft Windows NT 6.1.7600.0 
  5. CLR Version: 4.0.21006.1 

2 讀取CPU數量,內存容量

可以通過Windows Management Instrumentation (WMI)提供的接口讀取所需要的信息。

  1. private static UInt32 CountPhysicalProcessors() 
  2.      ManagementObjectSearcher objects = new ManagementObjectSearcher( 
  3.         “SELECT * FROM Win32_ComputerSystem”); 
  4.      ManagementObjectCollection coll = objects.Get(); 
  5.      foreach(ManagementObject obj in coll) 
  6.     { 
  7.         return (UInt32)obj[“NumberOfProcessors”]; 
  8.     } 
  9.     return 0; 
  10. private static UInt64 CountPhysicalMemory() 
  11.    ManagementObjectSearcher objects =new ManagementObjectSearcher( 
  12.       “SELECT * FROM Win32_PhysicalMemory”); 
  13.    ManagementObjectCollection coll = objects.Get(); 
  14.    UInt64 total = 0; 
  15.    foreach (ManagementObject obj in coll) 
  16.    { 
  17.        total += (UInt64)obj[“Capacity”]; 
  18.     } 
  19.     return total; 

請添加對程序集System.Management的引用,確保代碼可以正確編譯。

  1. Console.WriteLine(“Machine: {0}”, Environment.MachineName); 
  2. Console.WriteLine(“# of processors (logical): {0}”, Environment.ProcessorCount); 
  3. Console.WriteLine(“# of processors (physical): {0}”  CountPhysicalProcessors()); 
  4. Console.WriteLine(“RAM installed: {0:N0} bytes”,  CountPhysicalMemory()); 
  5. Console.WriteLine(“Is OS 64-bit? {0}”,   Environment.Is64BitOperatingSystem); 
  6. Console.WriteLine(“Is process 64-bit? {0}”,  Environment.Is64BitProcess); 
  7. Console.WriteLine(“Little-endian: {0}”, BitConverter.IsLittleEndian); 
  8. foreach (Screen screen in  System.Windows.Forms.Screen.AllScreens) 
  9.      Console.WriteLine(“Screen {0}”, screen.DeviceName); 
  10.      Console.WriteLine(“\tPrimary {0}”, screen.Primary); 
  11.      Console.WriteLine(“\tBounds: {0}”, screen.Bounds); 
  12.      Console.WriteLine(“\tWorking Area: {0}”,screen.WorkingArea); 
  13.      Console.WriteLine(“\tBitsPerPixel: {0}”,screen.BitsPerPixel); 

3 讀取注冊表鍵值對

  1. using (RegistryKey keyRun = Registry.LocalMachine.OpenSubKey(@”Software\Microsoft\Windows\CurrentVersion\Run”)) 
  2.     foreach (string valueName in keyRun.GetValueNames()) 
  3.     { 
  4.      Console.WriteLine(“Name: {0}\tValue: {1}”, valueName, keyRun.GetValue(valueName)); 
  5.     } 

請添加命名空間Microsoft.Win32,以確保上面的代碼可以編譯。

4 啟動,停止Windows服務

這項API提供的實用功能常常用來管理應用程序中的服務,而不必到控制面板的管理服務中進行操作。

  1. ServiceController controller = new ServiceController(“e-M-POWER”);      
  2. controller.Start();      
  3. if (controller.CanPauseAndContinue)      
  4. {      
  5.     controller.Pause();      
  6.     controller.Continue();      
  7. }      
  8. controller.Stop();       

.net提供的API中,可以實現一句話安裝與卸載服務

  1. if (args[0] == "/i") 
  2.        ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); 
  3. else if (args[0] == "/u") 
  4.    ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); 

如代碼所示,給應用程序傳入i或u參數,以表示是卸載或是安裝程序。




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved