C #10 practical code snippets that programmers often use (1)
1. Read the operating system and CLR versions.
- OperatingSystem os = System.Environment.OSVersion;
- Console.WriteLine(“Platform: {0}”, os.Platform);
- Console.WriteLine(“Service Pack: {0}”, os.ServicePack);
- Console.WriteLine(“Version: {0}”, os.Version);
- Console.WriteLine(“VersionString: {0}”, os.VersionString);
- Console.WriteLine(“CLR Version: {0}”, System.Environment.Version);
In my Windows 7 system, output the following information
- Platform: Win32NT
- Service Pack:
- Version: 6.1.7600.0
- VersionString: Microsoft Windows NT 6.1.7600.0
- 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.
- private static UInt32 CountPhysicalProcessors()
- {
- ManagementObjectSearcher objects = new ManagementObjectSearcher(
- “SELECT * FROM Win32_ComputerSystem”);
- ManagementObjectCollection coll = objects.Get();
- foreach(ManagementObject obj in coll)
- {
- return (UInt32)obj[“NumberOfProcessors”];
- }
- return 0;
- }
- private static UInt64 CountPhysicalMemory()
- {
- ManagementObjectSearcher objects =new ManagementObjectSearcher(
- “SELECT * FROM Win32_PhysicalMemory”);
- ManagementObjectCollection coll = objects.Get();
- UInt64 total = 0;
- foreach (ManagementObject obj in coll)
- {
- total += (UInt64)obj[“Capacity”];
- }
- return total;
- }
Add a reference to the assembly System. Management to ensure that the code can be compiled correctly.
- Console.WriteLine(“Machine: {0}”, Environment.MachineName);
- Console.WriteLine(“# of processors (logical): {0}”, Environment.ProcessorCount);
- Console.WriteLine(“# of processors (physical): {0}” CountPhysicalProcessors());
- Console.WriteLine(“RAM installed: {0:N0} bytes”, CountPhysicalMemory());
- Console.WriteLine(“Is OS 64-bit? {0}”, Environment.Is64BitOperatingSystem);
- Console.WriteLine(“Is process 64-bit? {0}”, Environment.Is64BitProcess);
- Console.WriteLine(“Little-endian: {0}”, BitConverter.IsLittleEndian);
- foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)
- {
- Console.WriteLine(“Screen {0}”, screen.DeviceName);
- Console.WriteLine(“\tPrimary {0}”, screen.Primary);
- Console.WriteLine(“\tBounds: {0}”, screen.Bounds);
- Console.WriteLine(“\tWorking Area: {0}”,screen.WorkingArea);
- Console.WriteLine(“\tBitsPerPixel: {0}”,screen.BitsPerPixel);
- }
3. Read the registry key-Value Pair
- using (RegistryKey keyRun = Registry.LocalMachine.OpenSubKey(@”Software\Microsoft\Windows\CurrentVersion\Run”))
- {
- foreach (string valueName in keyRun.GetValueNames())
- {
- Console.WriteLine(“Name: {0}\tValue: {1}”, valueName, keyRun.GetValue(valueName));
- }
- }
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.
- ServiceController controller = new ServiceController(“e-M-POWER”);
- controller.Start();
- if (controller.CanPauseAndContinue)
- {
- controller.Pause();
- controller.Continue();
- }
- controller.Stop();
. Net APIs can be installed and uninstalled in one sentence.
- if (args[0] == "/i")
- {
- ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
- }
- else if (args[0] == "/u")
- {
- 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.