Domanda

Sto cercando di riavviare il servizio da una macchina remota. Ecco il mio codice Il problema è che devo inserire startinfo.filename = " sc.exe " poiché sto inserendo " start / wait sc " questo sta causando un errore. Ecco il mio codice, ogni pensiero. Inoltre, se qualcuno ha idea di come mantenere aperta la finestra cmd dopo l'esecuzione, in modo da poter vedere il codice che è stato eseguito sarebbe fantastico.

string strCommandStop1;
string strCommandStop2;
string strCommandStart1;
string strCommandStart2;
string strServer = "\\" + txtServerName.Text;
string strDb1 = "SqlAgent<*>quot; + txtInsName.Text;
string strDb2 = "MSSQL<*>quot; + 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);
}
È stato utile?

Soluzione

Perché non usi Classe ServiceController ? In questo modo avresti un controllo molto migliore di quello che sta succedendo.

Altri suggerimenti

" avviare " è un comando intrinseco nella shell cmd.exe, quindi utilizzare cmd.exe come nome file;

ProcessStartInfo si = new ProcessStartInfo();
si.CreateNoWindow = true;
si.Arguments = "/c start /wait sc.exe " + server + " Stop " + database;
si.FileName = "cmd.exe";
Process.Start(si);

Inoltre, la successiva riassegnazione della proprietà Arguments sembra strana. È necessario chiamare Process.Start per ogni riga di comando che si desidera eseguire.

Penso che il tuo problema sia che l'oggetto startInfo chiamerà effettivamente:

sc.exe start /wait sc <strServer> Start <strDb1>

Inoltre, è necessario utilizzare sc invece di utilizzare la classe ServiceController dalla libreria System.ServiceProcess?

Grazie a tutti quelli con cui ho utilizzato la classe ServiceController e che ha reso tutto molto più semplice. Grazie per tutto il vostro aiuto. J

La console può rimanere aperta se si utilizza cmd / k e senza l'avvio che sembra aprire un nuovo cmd. quindi dovrebbe essere qualcosa come cmd / k sc.exe " + server + " Interrompi " + database;

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top