的Process.Start:如何让输出? [英] Process.start: how to get the output?

查看:161
本文介绍了的Process.Start:如何让输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的单声道/ .NET应用程序运行外部命令行程序。 例如,我想运行的男人codeR 。是否有可能:

  1. 要获得命令行shell的输出,并在我的文本框中写呢?
  2. 要得到的数值,显示随时间的进度条逝去?
解决方案

在创建进程目标设置的StartInfo 适当的:

  VAR PROC =新的Process {
    StartInfo的=新的ProcessStartInfo {
        文件名=PROGRAM.EXE
        参数=命令行参数的可执行文件,
        UseShellExecute =假,
        RedirectStandardOutput = TRUE,
        CreateNoWindow =真
    }
};
 

然后启动该进程并从中读取:

  proc.Start();
而(!proc.StandardOutput.EndOfStream){
    串线= proc.StandardOutput.ReadLine();
    //做一些与行
}
 

您可以使用 int.Parse() int.TryParse()转换字符串到数字值。您可能需要做一些字符串操作第一如果在您阅读的字符串无效数字字符。

I would like to run an external command line program from my Mono/.NET app. For example, I would like to run mencoder. Is it possible:

  1. To get the command line shell output, and write it on my text box?
  2. To get the numerical value to show a progress bar with time elapsed?

解决方案

When you create your Process object set StartInfo appropriately:

var proc = new Process {
    StartInfo = new ProcessStartInfo {
        FileName = "program.exe",
        Arguments = "command line arguments to your executable",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};

then start the process and read from it:

proc.Start();
while (!proc.StandardOutput.EndOfStream) {
    string line = proc.StandardOutput.ReadLine();
    // do something with line
}

You can use int.Parse() or int.TryParse() to convert the strings to numeric values. You may have to do some string manipulation first if there are invalid numeric characters in the strings you read.

这篇关于的Process.Start:如何让输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆