C#实时输出Youtube dl



我搜索了很多,但都没有成功。。。当控制台结束时,这段代码会给我结果。但我想要实时输出!

  • 有什么方法可以做到这一点吗
string text1 = "C:/Program Files/Sycho_DL/Downloader.exe -f 242+250 --merge-output-format mp4 -o " + DPath + "/%(title)s.%(ext)s ";
string me2me1 = text1 + urlBoxText2;
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:/Program Files/Sycho_DL/Downloader.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = me2me1;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
var process = Process.Start(startInfo);
process.Start();
process.WaitForExit();
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
richbox1.Text = line;
}

.NET中内置的System.Diagnostics.ProcessAPI可能很难使用。我建议使用一个包装它们的库;我最喜欢的是CliWrap。它有几种不同的方式来执行外部过程;基于拉的事件流";返回IAsyncEnumerable在这里会很好地工作,类似于:

using CliWrap;
using CliWrap.EventStream;
var cmd = Cli.Wrap("ExePathGoesHere").WithArguments("ArgumentsGoHere");
await foreach (var cmdEvent in cmd.ListenAsync())
{
switch (cmdEvent)
{
case StandardOutputCommandEvent stdOut:
// to do: append stdOut.Text to your text box
break;
case StandardErrorCommandEvent stdErr:
// to do: append stdErr.Text to your text box
break;
default:
break;
}
}

最新更新