C#如何读取带有参数的控制台输出 [英] C# How To Read Console Output With Parameters

查看:503
本文介绍了C#如何读取带有参数的控制台输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以运行控制台应用程序并将其输出的内容以C#中的字符串形式返回?

Is it possible to run a console application and get its outputted contents back as a string in C#?

我希望能够在运行控制台时使用参数app:

I want to be able to use parameters when running the console app:

c:\files\app.exe -a 1 -b 2 -c 3


推荐答案

这不是我今天读的最清晰的,只是假设你生成一个进程( Process.Start()?),并希望将其输出回你的程序。

This isn't the clearest thing I've read today, but I can only assume you're spawning a process (with Process.Start()?) and want to get it's output back into your program.

如果是, Process.StandardOutput 可能是你要找的。例如:

If so, Process.StandardOutput is probably what you're looking for. For example:

System.Diagnostics.ProcessStartInfo startInfo = 
    new System.Diagnostics.ProcessStartInfo(@"c:\files\app.exe",@"-a 1 -b 2 -c 3"); 
startInfo.UseShellExecute = false; 
startInfo.RedirectStandardOutput = true; 
Process p = Process.Start(startInfo);
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

这篇关于C#如何读取带有参数的控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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