0

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

我在在线论坛上阅读了很多关于它的内容,但非对我有用。

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

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

我也需要输出。

非常感谢任何帮助。

提前致谢。

4

2 回答 2

0

示例代码:

// 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 结果

于 2013-10-29T07:32:03.083 回答
0

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

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

于 2013-10-29T06:32:51.330 回答