1 讀取操作系統和CLR的版本
- 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);
在我的Windows 7系統中,輸出以下信息
- Platform: Win32NT
- Service Pack:
- Version: 6.1.7600.0
- VersionString: Microsoft Windows NT 6.1.7600.0
- CLR Version: 4.0.21006.1
2 讀取CPU數量,內存容量
可以通過Windows Management Instrumentation (WMI)提供的接口讀取所需要的信息。
- 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;
- }
請添加對程序集System.Management的引用,確保代碼可以正確編譯。
- 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 讀取注冊表鍵值對
- 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));
- }
- }
請添加命名空間Microsoft.Win32,以確保上面的代碼可以編譯。
4 啟動,停止Windows服務
這項API提供的實用功能常常用來管理應用程序中的服務,而不必到控制面板的管理服務中進行操作。
- ServiceController controller = new ServiceController(“e-M-POWER”);
- controller.Start();
- if (controller.CanPauseAndContinue)
- {
- controller.Pause();
- controller.Continue();
- }
- controller.Stop();
.net提供的API中,可以實現一句話安裝與卸載服務
- if (args[0] == "/i")
- {
- ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
- }
- else if (args[0] == "/u")
- {
- ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
- }
如代碼所示,給應用程序傳入i或u參數,以表示是卸載或是安裝程序。