日期:2014-05-18 浏览次数:20966 次
Process p = new Process(); p.StartInfo.FileName = "cmd.exe "; p.StartInfo.Arguments = "/k net start mssqlserver"; p.Start(); p.Close();
------解决方案--------------------
Process myProcess = new Process(); myProcess.StartInfo.FileName = "cmd.exe ";//DOS控制平台 myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.CreateNoWindow = true; myProcess.StartInfo.RedirectStandardInput = true; myProcess.StartInfo.RedirectStandardOutput = true; myProcess.StartInfo.RedirectStandardError = true; myProcess.Start(); StreamWriter sIn = myProcess.StandardInput;//标准输入流 sIn.AutoFlush = true; StreamReader sOut = myProcess.StandardOutput;//标准输入流 StreamReader sErr = myProcess.StandardError;//标准错误流 sIn.Write("net start mssqlserver" + System.Environment.NewLine);//DOS控制平台上的命令 sIn.Write("exit " + System.Environment.NewLine);//DOS控制平台上的命令 string s = sOut.ReadToEnd();//读取执行DOS命令后输出信息 string er = sErr.ReadToEnd();//读取执行DOS命令后错误信息 Console.WriteLine(s); Console.WriteLine(er); if (myProcess.HasExited == false) { myProcess.Kill(); } sIn.Close(); sOut.Close(); sErr.Close(); myProcess.Close();