Using C # to control the services of a remote computer

Source: Internet
Author: User

Http://www.cnblogs.com/onekey/archive/2004/08/25/36513.html

provides classes in. NET to display and control services on Windows systems, and to enable access to remote computer service services, such as the ServiceController class under the System.ServiceProcess namespace, System.Management the classes of some of the WMI operations below. Although the use of ServiceController can be very convenient to achieve the control of the service, but also intuitive, concise and easy to understand. But I think his functionality is probably a little bit more than just using WMI to manipulate a service, and it can be cumbersome to operate on multiple services and not be able to list the specific data for all the services in the system.         Here's how to use the System.Management component to manipulate services on remote and local computers. WMI provides a scalable, extensible management architecture as part of the Windows 2000 operating system. The Public Information Model (CIM) is an extensible, object-oriented architecture designed by the Distributed Management Task Standards Association (DMTF) for managing systems, networks, applications, databases, and devices. The Windows management specification, also known as CIM for Windows, provides a uniform way to access management information. If you need to get detailed WMI information, ask your reader to review MSDN.         The System.Management component provides access to a wide range of management information and management event collections related to system, device, and application setting checkpoints based on the Windows Management Instrumentation (WMI) architecture. But the above is not our most concern, the following is the topic we need to talk about. There is no doubt that we will refer to the SYSTEM.MANAGEMENT.DLL assembly and use the classes under the System.Management namespace, such as Managementclass,managementobject. The following is a class called Win32servicemanager that wraps some of the relevant operations of the service, the code reads 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 the 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;
}
Gets the value of the specified service property
public Object Getservicevalue (String servicename,string PropertyName)
{
ManagementObject mo = This.managementClass.CreateInstance ();
Mo. Path = new Managementpath (this.strpath+). Name= "" +servicename+ "" "" ");
return Mo[propertyname];
}
Get all the service data for the connected computer
public string [,] Getservicelist ()
         

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.