Stop or turn on system service

zhaozj2021-02-16  95

Using system;

Using system.management;

Namespace arli.commonprj {

Public Class PLMM007_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 ("Win32_Service");

Private readonly managementObjectCollection moc;

PUBLIC PLMM007_WMI () {

MOC = mc.getinstances ();

}

///

/// query service mode /////

// To query the service, indicate all of the disabled services for NULL

Public string getDisabled (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;}

}

}

Arli.commonprj.plmm007_wmi o = new arli.commonprj.plmm007_wmi ();

//, such as the clipboard set to automatically start

O. ChangeTo ("Clipsrv", "Automatic");

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

New Post(0)