Process.start:如何获得输出? [英] Process.start: how to get the output?

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

问题描述

我想从我的 Mono/.NET 应用程序运行外部命令行程序.例如,我想运行 mencoder.有没有可能:

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. 获取命令行 shell 输出,并将其写在我的文本框中?
  2. 获取数值以显示经过时间的进度条?

推荐答案

当你适当地创建你的 Process 对象集 StartInfo 时:

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
}

您可以使用 int.Parse()int.TryParse() 将字符串转换为数值.如果您读取的字符串中存在无效数字字符,您可能需要先进行一些字符串操作.

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天全站免登陆