C #10 practical code snippets that programmers often use (1)

Source: Internet
Author: User

C #10 practical code snippets that programmers often use (1)

1. Read the operating system and CLR versions.

 
 
  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); 

In my Windows 7 system, output the following information

 
 
  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. Number of read CPUs and memory capacity

You can use the interface provided by Windows Management Instrumentation (WMI) to read the required information.

 
 
  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; 

Add a reference to the assembly System. Management to ensure that the code can be compiled correctly.

 
 
  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. Read the registry key-Value Pair

 
 
  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.     } 

Add the namespace Microsoft. Win32 to ensure that the above Code can be compiled.

4. Start and Stop the Windows Service

The utility provided by this API is often used to manage services in an application, instead of performing operations in the management service of the control panel.

 
 
  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 APIs can be installed and uninstalled in one sentence.

 
 
  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 }); 

As shown in the code, input an I or u parameter to the application to uninstall or install the application.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.