Use C # to operate remote services such as MSSQLServer, etc.

xiaoxiao2021-03-06  19

Some classes are provided in .NET to display and control the services on the Windows system, you can implement access to the remote computer service service, such as the serviceController class under the System.ServiceProcess Namespace, some WMI operations below System.Management . Although it can be easily controlled by ServiceController, it is very intuitive, simple and easy to understand. But I think that his functionality is a single single, and it may be a bit a single, and the operation of multiple services may be more troublesome, and the specific data of all services in the system cannot be listed. What you want to talk about is how to use the System.Management component to operate the services on the remote and local computers. As part of the Windows 2000 operating system, WMI provides a scalable, scalable management architecture. Public Information Model (CIM) is an extensible, object-oriented architecture designed by the Distributed Management Task Standards Association (DMTF), For management systems, networks, applications, databases, and devices. The Windows management specification is also known as Cim for Windows, providing unified access to management information. If you need to get a detailed WMI message, please check the MSDN. The System.Management component provides access to a large number of management information and management events, which are related to the system, devices, and application settings of the system, device, and application in accordance with the Windows Management Specification (WMI) structure. But the above is not our most concerned, the following is the topic we need to talk. There is no doubt that we have to reference the System.Management.dll assembly and use the class under the system.management namespace, such as managementClass, ManagementObject, etc.

Below some operations and services associated with a wrapper for a class named Win32ServiceManager code is as follows: using System; using System.Management; namespace ZZ.Wmi {public class Win32ServiceManager {private string strPath; private ManagementClass managementClass; public Win32ServiceManager () : this (".", null, null) {} public wellic, string username, string password {this.strpath = " host " // root // CIMv2: win32_service "; this.managementClass = New ManagementClass (strPath); if (! userName = null && userName.Length> 0) {ConnectionOptions connectionOptions = new ConnectionOptions (); connectionOptions.Username = userName; connectionOptions.Password = password; ManagementScope managementScope = new ManagementScope ( "" host "// root // cimv2 ", connectionOptions); this.managementClass.Scope = managementScope;}} // verify that can connect to a remote computer public static bool RemoteConnectValidate (string host, string userName, string password) {ConnectionOptions connectionOptions = new ConnectionOptions () ConnectionOptions.Username = username; connectionOptions .Password = password; ManagementScope managementScope = new ManagementScope ( "" host "// root // cimv2", connectionOptions); try {managementScope.Connect ();} catch {} return managementScope.IsConnected;} // Get the specified service value of the property public object GetServiceValue (string serviceName, string propertyName) {ManagementObject mo = this.managementClass.CreateInstance (); mo.Path = new ManagementPath ( ". Name = /" this.strPath " serviceName " / ""); Return Mo [PropertyName];} // Get all service data of the connected computer PUBLIC STRING [,] getServiceList () {string [,] service =

NEW STRING [this.ManagementClass.GetInstances (). count, 4]; int i = 0; foreach (managementObject mo in this.managementclass.getinstances ()) {Services [i, 0] = (string) Mo ["name" ]; Services [i, 1] = (String) Mo ["DisplayName"]; Services [i, 2] = (string) Mo ["State"]; Services [i, 3] = (string) Mo ["startmode "]; i ;}} // Get the specified service data of the connected computer PUBLIC STRING [,] GetServiceList (String ServerName) {Return GetServiceList (New String [] {servername});} // Get the connected Specified service data for computer PUBLIC STRING [,] getServiceList (String [] ServerNames) {string [,] services = new string [ServerNames.Length, 4]; managementObject Mo = this.managementClass.createInstance (); for (int i = 0; i

This.ManagementClass.createInstance (); mo.path = new managementPath (this.strpath ". name = /" " serviceName " / "); try {// Judgment Can Pause IF ((Bool) Mo [" Acceptpause "] && (String) Mo [" State "] ==" Running ") Mo.invokeMethod (" Pauseservice ", NULL);} catch (management) {strrst = E.MESSAGE;} Return strrst;} // Restore specified service public string ResumeService (string serviceName) {string strRst = null; ManagementObject mo = this.managementClass.CreateInstance (); ". Name = /" mo.Path = new ManagementPath (this.strPath " serviceName " / "" ); try {// Determines if IF ((BOOL) MO ["AcceptPause"] && (String) Mo ["State"] == "paused") Mo.invokeMethod ("resuMeservice", null);} catch (Managementexception e) {strrst = E.Message;} Return strrs}}}}}}} The above content is from http://www.20cn.net/ns/wz/sys/data/20040521140305.htm

In addition, the "stop service" mentioned in the comment information is incorrect, and the code should be processed as the start-up service, and the code is processed as follows:

public string StopService (string serviceName) {string strRst = null; ManagementObject mo = this.managementClass.CreateInstance (); mo.Path = new ManagementPath (this.strPath " serviceName " / "" "Name = /."); try {IF (String) MO ["State"]! = "Stopped") Mo.invokeMethod ("stopservice", null);} catch (management = E) {strrst = E.MESSAGE;} Return Strrst;}

转载请注明原文地址:https://www.9cbs.com/read-46875.html

New Post(0)