winform执行dos命令在textarea里实时输出

csharp winform 文章 2022-03-17 14:53 816 0 全屏看文

AI助手支持GPT4.0

private void processCmd(String cmd)
{
	Control.CheckForIllegalCrossThreadCalls = false;
	process = new Process();
	process.StartInfo.FileName = "cmd.exe";
	process.StartInfo.WorkingDirectory = ".";
	process.StartInfo.UseShellExecute = false;
	process.StartInfo.RedirectStandardInput = true;
	process.StartInfo.RedirectStandardOutput = true;
	process.StartInfo.CreateNoWindow = true;

	//Process.Start("cmd.exe");
	process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);

	process.Start();

	process.StandardInput.WriteLine(cmd);

	//process.StandardInput.WriteLine("exit");

	process.BeginOutputReadLine();
	using (StreamWriter sw = new StreamWriter("output.log"))
	{
	 sw.WriteLine(process.StandardOutput.ReadToEnd());
	}

}
private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
	if (!String.IsNullOrEmpty(outLine.Data))
	{
		this.Invoke(ReadStdOutput, new object[] { outLine.Data });
	}
}


-EOF-

AI助手支持GPT4.0