Executing sc.exe from .NET Process unable to start and stop service.

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2009-07-31T12:20:32Z Indexed on 2010/06/11 9:13 UTC
Read the original article Hit count: 172

I'm trying to restart the service from a remote machine. Here is my code. The problem is that I need to enter startinfo.filename = "sc.exe" since I'm putting "start /wait sc" this is causing an error. Here is my code, any thoughts. Also if anyone has any idea how to keep the cmd window open after this is ran so I could see the code that was ran that would be awesome.

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

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about windows-services