我正在尝试从远程计算机重新启动该服务。这是我的代码。问题是我需要输入 startinfo.filename =" sc.exe" ,因为我正在输入" start / wait sc" 这导致错误。这是我的代码,任何想法。此外,如果有人知道如何在运行之后保持cmd窗口打开,那么我可以看到运行的代码非常棒。

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);
}
有帮助吗?

解决方案

为什么不使用 ServiceController 类?通过这种方式,您可以更好地控制正在发生的事情。

其他提示

&QUOT;开始&QUOT;是cmd.exe shell中的内部命令,因此请使用cmd.exe作为文件名;

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

此外,您连续重新分配Arguments属性看起来很奇怪。您需要为要执行的每个命令行调用Process.Start。

我认为你的问题是你的startInfo对象实际上会调用:

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

此外,是否有特殊要求您必须使用sc而不是使用System.ServiceProcess库中的ServiceController类?

感谢所有使用ServiceController类的人,这让一切变得如此简单。感谢你的帮助。 Ĵ

如果你使用cmd / k而没有启动似乎打开一个新的cmd,控制台可以保持打开状态。 所以它应该像cmd / k sc.exe“ +服务器+“停止“ +数据库;

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top