【问题标题】:Getting output from CMD prompt从 CMD 提示符获取输出
【发布时间】:2013-10-29 06:27:28
【问题描述】:

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

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

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

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

我也需要输出。

非常感谢任何帮助。

提前致谢。

【问题讨论】:

  • 您能否发布有效的批处理文件的详细信息
  • 环境变量必须在您从命令提示符运行时可用,而在从代码执行时不可用。您能分享从应用程序运行时看到的错误吗?
  • “它不起作用”不是错误描述。我们无法以这种方式帮助您。发布详细信息。
  • 这个请,因为我们无法解决您的问题:stackoverflow.com/questions/how-to-ask
  • 嗨,对不起,伙计们。我发现了错误。我的 m/c 有多个版本的 java。当我从 cmd 执行时,它工作正常。但是当我过去从代码/exe执行时它失败了,因为它使用了不同版本的java。经过一番研发,我发现了这个问题,并通过使用java的绝对路径解决了这个问题。谢谢大家的帮助。 :)

标签: c# .net batch-file


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    示例代码:

    // 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();
    

    这个问题可能对你有帮助 :How To: Execute command line in C#, get STD OUT results

    【讨论】:

      猜你喜欢
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 2017-01-31
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      相关资源
      最近更新 更多