Как управлять службами Windows через WMI? - C#
Формулировка задачи:
Помогите написать Windows-форму (проект Windows Form Application), которая выводит список всех служб с помощью элементов WMI-таблицы win32_service и позволяет остановить любую службу с помощью WMI-метода StopService.
Решение задачи: «Как управлять службами Windows через WMI?»
textual
Листинг программы
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Management; using System.Management.Instrumentation; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { txtb.Text = "--- Win Process ---" + "\r\n"; win_process(); txtb.Text = txtb.Text + "\r\n" + "--- Win Soft and Date Install ---" + "\r\n"; win_soft(); txtb.Text = txtb.Text + "\r\n" + "--- Win Services ---" + "\r\n"; win_services(); } private void win_process() { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "Select Name, CommandLine From Win32_Process"); foreach (ManagementObject instance in searcher.Get()) { txtb.Text = txtb.Text + "\r\n" + instance["Name"]; } } private void win_soft() { ManagementObjectSearcher searcher_soft = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Product"); foreach (ManagementObject queryObj in searcher_soft.Get()) { txtb.Text = txtb.Text + "\r\n" + queryObj["Caption"] + " - " + queryObj["InstallDate"].ToString().Remove(0, 6) + "." + queryObj["InstallDate"].ToString().Remove(0, 4).Remove(2) + "." + queryObj["InstallDate"].ToString().Remove(4); } } private void win_services() { ManagementObjectSearcher searcher3 = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Service"); foreach (ManagementObject queryObj in searcher3.Get()) { txtb.Text = txtb.Text + "\r\n" + queryObj["Caption"] + " - " + queryObj["Description"] + " - " + queryObj["DisplayName"] + " - " + queryObj["Name"] + " - " + queryObj["PathName"] + " - " + queryObj["Started"]; } } private void win_info() { ManagementObjectSearcher searcher5 = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_OperatingSystem"); foreach (ManagementObject queryObj in searcher5.Get()) { Console.WriteLine("-----------------------------------"); Console.WriteLine("Win32_OperatingSystem instance"); Console.WriteLine("-----------------------------------"); Console.WriteLine("BuildNumber: {0}", queryObj["BuildNumber"]); Console.WriteLine("Caption: {0}", queryObj["Caption"]); Console.WriteLine("FreePhysicalMemory: {0}", queryObj["FreePhysicalMemory"]); Console.WriteLine("FreeVirtualMemory: {0}", queryObj["FreeVirtualMemory"]); Console.WriteLine("Name: {0}", queryObj["Name"]); Console.WriteLine("OSType: {0}", queryObj["OSType"]); Console.WriteLine("RegisteredUser: {0}", queryObj["RegisteredUser"]); Console.WriteLine("SerialNumber: {0}", queryObj["SerialNumber"]); Console.WriteLine("ServicePackMajorVersion: {0}", queryObj["ServicePackMajorVersion"]); Console.WriteLine("ServicePackMinorVersion: {0}", queryObj["ServicePackMinorVersion"]); Console.WriteLine("Status: {0}", queryObj["Status"]); Console.WriteLine("SystemDevice: {0}", queryObj["SystemDevice"]); Console.WriteLine("SystemDirectory: {0}", queryObj["SystemDirectory"]); Console.WriteLine("SystemDrive: {0}", queryObj["SystemDrive"]); Console.WriteLine("Version: {0}", queryObj["Version"]); Console.WriteLine("WindowsDirectory: {0}", queryObj["WindowsDirectory"]); } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } private void win_HDD_info() { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Volume"); foreach (ManagementObject queryObj in searcher.Get()) { Console.WriteLine("-----------------------------------"); Console.WriteLine("Win32_Volume instance"); Console.WriteLine("-----------------------------------"); Console.WriteLine("Capacity: {0}", queryObj["Capacity"]); Console.WriteLine("Caption: {0}", queryObj["Caption"]); Console.WriteLine("DriveLetter: {0}", queryObj["DriveLetter"]); Console.WriteLine("DriveType: {0}", queryObj["DriveType"]); Console.WriteLine("FileSystem: {0}", queryObj["FileSystem"]); Console.WriteLine("FreeSpace: {0}", queryObj["FreeSpace"]); } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } private void win_Lan_info() { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_NetworkAdapterConfiguration"); foreach (ManagementObject queryObj in searcher.Get()) { Console.WriteLine("--------- Win32_NetworkAdapterConfiguration instance --------------"); Console.WriteLine("Caption: {0}", queryObj["Caption"]); if (queryObj["DefaultIPGateway"] == null) Console.WriteLine("DefaultIPGateway: {0}", queryObj["DefaultIPGateway"]); else { String[] arrDefaultIPGateway = (String[])(queryObj["DefaultIPGateway"]); foreach (String arrValue in arrDefaultIPGateway) { Console.WriteLine("DefaultIPGateway: {0}", arrValue); } } if (queryObj["DNSServerSearchOrder"] == null) Console.WriteLine("DNSServerSearchOrder: {0}", queryObj["DNSServerSearchOrder"]); else { String[] arrDNSServerSearchOrder = (String[])(queryObj["DNSServerSearchOrder"]); foreach (String arrValue in arrDNSServerSearchOrder) { Console.WriteLine("DNSServerSearchOrder: {0}", arrValue); } } if (queryObj["IPAddress"] == null) Console.WriteLine("IPAddress: {0}", queryObj["IPAddress"]); else { String[] arrIPAddress = (String[])(queryObj["IPAddress"]); foreach (String arrValue in arrIPAddress) { Console.WriteLine("IPAddress: {0}", arrValue); } } if (queryObj["IPSubnet"] == null) Console.WriteLine("IPSubnet: {0}", queryObj["IPSubnet"]); else { String[] arrIPSubnet = (String[])(queryObj["IPSubnet"]); foreach (String arrValue in arrIPSubnet) { Console.WriteLine("IPSubnet: {0}", arrValue); } } Console.WriteLine("MACAddress: {0}", queryObj["MACAddress"]); Console.WriteLine("ServiceName: {0}", queryObj["ServiceName"]); } } private void win_Video_info() { ManagementObjectSearcher searcher11 = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_VideoController"); foreach (ManagementObject queryObj in searcher11.Get()) { Console.WriteLine("----------- Win32_VideoController instance -----------"); Console.WriteLine("AdapterRAM: {0}", queryObj["AdapterRAM"]); Console.WriteLine("Caption: {0}", queryObj["Caption"]); Console.WriteLine("Description: {0}", queryObj["Description"]); Console.WriteLine("VideoProcessor: {0}", queryObj["VideoProcessor"]); } } private void win_Processor_info() { ManagementObjectSearcher searcher8 = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor"); foreach (ManagementObject queryObj in searcher8.Get()) { Console.WriteLine("------------- Win32_Processor instance ---------------"); Console.WriteLine("Name: {0}", queryObj["Name"]); Console.WriteLine("NumberOfCores: {0}", queryObj["NumberOfCores"]); Console.WriteLine("ProcessorId: {0}", queryObj["ProcessorId"]); } } private void win_Memory_info() { ManagementObjectSearcher searcher12 = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PhysicalMemory"); Console.WriteLine("------------- Win32_PhysicalMemory instance --------"); foreach (ManagementObject queryObj in searcher12.Get()) { Console.WriteLine("BankLabel: {0} ; Capacity: {1} Gb; Speed: {2} ", queryObj["BankLabel"], Math.Round(System.Convert.ToDouble(queryObj["Capacity"]) / 1024 / 1024 / 1024, 2), queryObj["Speed"]); } } private void win_HDD_summary_info() { ManagementObjectSearcher searcher13 = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskDrive"); Console.WriteLine("--------- Win32_DiskDrive instance ---------------"); foreach (ManagementObject queryObj in searcher13.Get()) { Console.WriteLine("DeviceID: {0}; InterfaceType: {1}; Manufacturer: {2}; Model: {3}; SerialNumber: {4}; Size: {5} Gb", queryObj["DeviceID"], queryObj["InterfaceType"], queryObj["Manufacturer"], queryObj["Model"], queryObj["SerialNumber"], Math.Round(System.Convert.ToDouble(queryObj["Size"]) / 1024 / 1024 / 1024, 2)); Console.WriteLine("-----"); } } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д