Use SC to get the startup type of Windows service in C #

Source: Internet
Author: User

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;
           }
       } 

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.