Gets the current state of the startup type of the service, returning the state of the startup type
2: Automatic, 3: Manual, 4: Disabled ...
Sservicename Service Name
Sstate output related status code, failure is error message
public int Getwindowsservicestarttype (string sservicename, out string sstate)
{
Sstate = "";
Try
{
System.Diagnostics.ProcessStartInfo Objprocessinf = new System.Diagnostics.ProcessStartInfo ();
Objprocessinf.filename = "cmd.exe";
Objprocessinf.useshellexecute = false;
Objprocessinf.redirectstandarderror = true;
Objprocessinf.redirectstandardinput = true;
Objprocessinf.redirectstandardoutput = true;
Objprocessinf.createnowindow = true;
Objprocessinf.arguments = "/C SC QC" + sservicename;
System.Diagnostics.Process objprocess = System.Diagnostics.Process.Start (Objprocessinf);
String Sstatevalue = ObjProcess.StandardOutput.ReadToEnd ();
if (Sstatevalue.indexof ("Auto_start") > 0)
{
Sstate = "Auto_start";
return 2;
}
if ( Sstatevalue.indexof ("Demand_start") > 0)
{
sstate = "Demand_start";
return 3;
}
if (Sstatevalue.indexof ("DISABLED") > 0)
{
Sstate = "DISABLED";
return 4;
}
return 0;
}
catch (Exception e)
{
Sstate = E.message;
return-1;
}
}
To set the startup type of a service
Sservicename Service Name
Istarttype the startup type you want to set
Return successful status true: Success, false: Failed
Public Boolean Setwindowsservicestarttype (String sservicename, int istarttype)
{
Try
{
System.Diagnostics.ProcessStartInfo Objprocessinf = new System.Diagnostics.ProcessStartInfo ();
Objprocessinf.filename = "cmd.exe";
Objprocessinf.createnowindow = false;
Objprocessinf.windowstyle = System.Diagnostics.ProcessWindowStyle.Hidden;
String sstartstate = "Boot";
Switch (istarttype)
{
Case 1:
{
Sstartstate = "System";
Break
}
Case 2:
{
Sstartstate = "Auto";
Break
}
Case 3:
{
Sstartstate = "demand";
Break
}
Case 4:
{
Sstartstate = "Disabled";
Break
}
Default
{
Break
}
}
Objprocessinf.arguments = "/C sc config" + sservicename + "start=" + sstartstate;
System.Diagnostics.Process.Start (Objprocessinf);
return true;
}
Catch
{
return false;
}
}