Using system;
Using system.management;
Namespace arli.commonprj {
Public class service_wmi {
Public Static Readonly System.version Myversion = New System.Version (1, 1);
Private managementBaseObject iobj = null;
Private managementBaseObject Oobj = NULL;
Private managementClass MC = New ManagementClass (@ "// arli / win32 / service");
Private readonly managementObjectCollection moc;
Public service_wmi () {
MOC = mc.getinstances ();
}
///
/// Remote WMI construction ///
/// Computer name
/// User name, domain users can use UserName @ domain
/// password
Public service_wmi (String RemoteComputername, String Username, String Password) {
MOC = GetServiceCollection (RemoteComputerName, UserName, Password);
}
Private ManagementObjectCollection GetServiceCollection (String RemoteComputername, String UserName, String Password) {
ManagementObjectCollection ReturnWmicollection = NULL;
ConnectionOptions Co = New ConnectionOptions ();
Co. HERNAME = UserName;
Co.password = password;
System.Management.ManagementScope MS = New System.Management.ManagementsCope (" RemoteComputername " // root // CIMv2 ", CO);
System.Management.ObjectQuery oq = new system.management.ObjectQuery ("SELECT * WIN32_SERVICE");
ManagementObjectSearcher Query = New ManagementObjectSearcher (MS, OQ);
Returnwmicollection = query.get ();
Return ReturnWmicollection;
}
///
/// query service mode /////
// To query the service, indicate all of the disabled services for NULL
Public String getStartMode (String ServiceName) {
String s = "";
Foreach (ManagementObject Mo in MoC) {
IF (serviceName! = null) {// Service If not null
IF ((String) Mo ["Name"]). TouPper () == ServiceName.toupper ()) {
Return (String) Mo ["startmode"];
}
IF (String) Mo ["startmode"] == "disabled") {
S = Mo ["name"]. TOSTRING () "/ r / n"; // I have changed into the desired format
// You can use displayName, above is ServiceName, below this is DisplayName
// s = mo ["displayName"]. TOSTRING () "/ r / n";
}
}
Return S;
}
///
/// Setting mode ////
/// Service Name
/// mode, such as: boot / system / automatic / manual / disabled
///
Operation completion 1, otherwise 0
Public int Changeto (String Servicename, String Startmode) {
Foreach (ManagementObject Mo in MoC) {
// Service name existence
IF ((String) Mo ["Name"]). TouPper () == ServiceName.toupper ()) {
// If the service is already a specified mode, return
IF (String) Mo ["StartMode"] == startMode) Return 0;
// Set service mode
Iobj = mtMethodparameters ("ChangeStartMode");
Iobj ["startmode"] = startmode;
OOBJ = Mo.invokeMethod ("ChangeStartMode", IOBJ, NULL);
Return 1;
}
}
Return 0;
}
}
}