Skip to content

Need help to start sc.exe from c# and pass parms in quotes

Robert Bone

Asked May 19, 2009, 2:03 PM EDT3 answers
Share
LinkedInX

1.  In a command window, I would type: sc config "Bonjour Service" start= "manual"

2.  How do I do this from within a C# program?

3.  Do I need to stop anything when this has finished?  (In the command window it sim,ply stops after it has processed my request).

 

THANKS!!!

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    hey,

    try to use the proccesStartInfo:

    like this:

                Process proc = new Process(); //call new Process
                ProcessStartInfo info = new ProcessStartInfo(); //call new ProcessStartInfo
                info.Arguments = "config \"Bonjour Service\" start= \"manual\""; //set the arguments
                info.FileName = "sc.exe"; //set the file name (location)
                proc.StartInfo = info; //put the StartInfo into the Procces method
                proc.Start(); //Start the procces (sc.exe with the arguments)
                proc.WaitForExit(); //waits till the procces is done

    i hope this helped

    dont forget :   using System.Diagnostics;
    0
  • 0
    Wahoo!  I'm good to go!  THANKS!
    0
  • 0
    I'm trying to do something similar I have for commands that I need to run using sc.exe the problem is I need to have start /wait in front of the file name so it appears such as this.  strCommandStop1 = @"start /wait sc " + strServer + " Stop " + strDb1; because of this I can't say StartInfo.FileName = "sc.exe".

    string strCommandStop1;
                string strCommandStop2;
                string strCommandStart1;
                string strCommandStart2;
                string strServer = "\\" + txtServerName.Text;
                string strDb1 = "SqlAgent$" + txtInsName.Text;
                string strDb2 = "MSSQL$" + txtInsName.Text;

                strCommandStop1 = @"start /wait sc " + strServer + " Stop " + strDb1;
                strCommandStop2 = @"start /wait sc " + strServer + " Stop " + strDb2;
                strCommandStart1 = @"start /wait sc " + strServer + " Start " + strDb2;
                strCommandStart2 = @"start /wait sc " + strServer + " Start " + strDb1;

                try
                {
                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.CreateNoWindow = true;
                   
                    startInfo.Arguments = strCommandStop1;
                    startInfo.Arguments = strCommandStop2;
                    startInfo.Arguments = strCommandStart1;
                    startInfo.Arguments = strCommandStart2;
                    startInfo .FileName = "sc.exe";

                    Process.Start(startInfo);
                   
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
    Any thoughts on what I could change to make this work. I'm trying to restart a service from a remote machine on the network.

    Thanks
    0