Use C # To Control Remote Computer Services

Source: Internet
Author: User

In. net provides some classes to display and control services on Windows systems, and can access remote computer services, such as System. serviceController class under the ServiceProcess namespace, System. management. Although

ServiceController can easily control services, and is intuitive, concise, and easy to understand. However, I think its functions may be a bit simpler than WMI for service operations, and it may be troublesome to operate on multiple services, the data of all services in the system cannot be listed. Here we will talk about how to use the System. Management component to operate services on remote and local computers.

As part of the Windows 2000 operating system, WMI provides a scalable and scalable management architecture. the Common Information Model (CIM) is a scalable and object-oriented architecture designed by the Distributed Management Task Standards Association (DMTF, used to manage systems, networks, applications, databases, and devices. Windows Management specifications, also known as CIM for Windows, provide a unified way to access and manage information. For more information about WMI, see MSDN. System. the Management component provides access to a large number of Management information and Management event sets, which are related to Setting Detection Points for systems, devices, and applications according to the Windows Management Specification (WMI) structure.

But the above is not what we are most concerned about. The following are the topics we need to talk about.

Undoubtedly, we need to reference the System. Management. Dll assembly and use the classes in the System. Management namespace, such as ManagementClass and ManagementObject. The following uses a class named Win32ServiceManager to encapsulate some operations related to the service. The 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 Win32ServiceManager (string host, 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 you 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;
}
// Obtain the value of the specified service attribute
Public object GetServiceValue (string serviceName, string propertyName)
{
ManagementObject mo = this. managementClass. CreateInstance ();
Mo. Path = new ManagementPath (this. strPath + ". Name =" "+ serviceName + """);
Return mo [propertyName];
}
// Obtain all service data of the connected computer
Public string [,] GetServiceList ()
{
String [,] services = 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 ++;
}
Return services;
}
// Obtain the specified service data of the connected computer
Public string [,] GetServiceList (string serverName)
{

Related Article

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.