我想中断通过 cmd.exe 运行的命令。在下面的代码中,我以 ping www.stackoverflow.com -t 为例。
public void Run()
{
System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo("cmd.exe");
si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
si.RedirectStandardError = true;
si.UseShellExecute = false;
si.CreateNoWindow = false;
//si.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process console = new Process();
console.StartInfo = si;
console.EnableRaisingEvents = true;
console.OutputDataReceived += proc_OutputDataReceived;
console.ErrorDataReceived += proc_ErrorDataReceived;
console.Start();
console.BeginOutputReadLine();
console.BeginErrorReadLine();
console.StandardInput.WriteLine("ping www.stackoverflow.com -t");
Thread.Sleep(4000);
bool success = GenerateConsoleCtrlEvent((uint)0, (uint)console.SessionId);
if (!success)
{
MessageBox.Show("Error Code: " + Marshal.GetLastWin32Error().ToString());
}
char asdf = (char)0x3;
console.StandardInput.WriteLine('\x3');
console.StandardInput.WriteLine(asdf);
console.StandardInput.Write(asdf);
//console.StandardInput.Close();
console.StandardInput.WriteLine(@"exit");
console.WaitForExit();
}
GenerateConsoleCtrlEvent 的错误代码是 6。
我已按照以下说明进行操作:
但是,我无法中断该过程。
非常感谢任何帮助,