最佳的方式来调用C#外部程序,并解析输出 [英] Best Way to call external program in c# and parse output

查看:230
本文介绍了最佳的方式来调用C#外部程序,并解析输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

复制

  

<一个href="http://stackoverflow.com/questions/415620/redirect-console-output-to-textbox-in-separate-program-c">http://stackoverflow.com/questions/415620/redirect-console-output-to-textbox-in-separate-program-c   <一href="http://stackoverflow.com/questions/353601/capturing-nslookup-shell-output-with-c">http://stackoverflow.com/questions/353601/capturing-nslookup-shell-output-with-c

我希望从我的C#code调用外部程序。

该计划,我呼吁,让我们说foo.exe的返回大约12行文字。

我要调用的程序,并解析直通输出。

什么是做到这一点的最优化的方式?

code段也AP preciated:)

非常感谢你。

解决方案

 使用系统;
使用System.Diagnostics程序;

公共类RedirectingProcessOutput
{
    公共静态无效的主要()
    {
        进程p =新的Process();
        p.StartInfo.FileName =cmd.exe的;
        p.StartInfo.Arguments =/ C DIR *的.cs;
        p.StartInfo.UseShellExecute = FALSE;
        p.StartInfo.RedirectStandardOutput = TRUE;
        p.Start();

        字符串输出= p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        Console.WriteLine(输出);
        Console.WriteLine(输出);
    }
}
 

Duplicate

http://stackoverflow.com/questions/415620/redirect-console-output-to-textbox-in-separate-program-c http://stackoverflow.com/questions/353601/capturing-nslookup-shell-output-with-c

I am looking to call an external program from within my c# code.

The program I am calling, lets say foo.exe returns about 12 lines of text.

I want to call the program and parse thru the output.

What is the most optimal way to do this ?

Code snippet also appreciated :)

Thank You very much.

解决方案

using System;
using System.Diagnostics;

public class RedirectingProcessOutput
{
    public static void Main()
    {
        Process p = new Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = "/c dir *.cs";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.Start();

        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        Console.WriteLine("Output:");
        Console.WriteLine(output);    
    }
}

这篇关于最佳的方式来调用C#外部程序,并解析输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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