gpt4 book ai didi

c# - 在 C# 中调用外部程序并解析输出的最佳方式

转载 作者:IT王子 更新时间:2023-10-29 04:23:08 24 4
gpt4 key购买 nike

重复

Redirect console output to textbox in separate programCapturing nslookup shell output with C#

我希望从我的 C# 代码中调用外部程序。

我正在调用的程序,假设 foo.exe 返回大约 12 行文本。

我想调用程序并解析输出。

执行此操作的最佳方法是什么?

代码片段也很受欢迎:)

非常感谢。

最佳答案

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# - 在 C# 中调用外部程序并解析输出的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/878632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com