从CMD提示符获取输出

本文关键字:输出 获取 提示符 CMD | 更新日期: 2023-09-27 18:37:18

在我最近的任务中,我需要执行一个java命令并从中获取一些值,并在我的程序中使用它。

我在在线论坛上读了很多关于它的信息,但没有对我有用。

我尝试创建它的bat文件并在我的程序中执行它,但这也不起作用。

当我在命令提示符下执行它或直接执行 bat 文件时,它可以工作。但是当我从应用程序/exe 执行时,它会失败。

我也需要输出。

任何帮助都非常感谢。

提前谢谢。

从CMD提示符获取输出

最好的方法是使用进程类,它将允许您捕获标准输出并将输入发送到应用程序。

http://msdn.microsoft.com/en-us/library/System.Diagnostics.Process(v=vs.110).aspx

示例代码:

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "YOURBATCHFILE.bat";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

这个问题可能会帮助你:如何:在C#中执行命令行,获取STD OUT结果