如何在Xamarin.Mac中执行终端命令并读入其输出 [英] How to execute a terminal command in Xamarin.Mac and read-in its output

查看:105
本文介绍了如何在Xamarin.Mac中执行终端命令并读入其输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在编写Xamarin.Mac应用程序.我们需要执行正常运行时间"之类的命令,并将其输出读取到应用程序中进行解析.

We are writing a Xamarin.Mac application. We need to execute a command like "uptime" and read it's output into an application to parse.

可以这样做吗?在Swift和Objective-C中有NTask,但我似乎无法在C#中找到任何示例.

Could this be done? In Swift and Objective-C there is NTask, but I don't seem to be able to find any examples in C#.

推荐答案

在Mono/Xamarin.Mac下,您可以将标准" .Net/C#进程类映射为底层OS(对于OS-X Mono,MonoMac和Xamarin.Mac,以及* nix的Mono).

Under Mono/Xamarin.Mac, you can the "standard" .Net/C# Process Class as the Process gets mapped to the underlaying OS (OS-X For Mono, MonoMac and Xamarin.Mac, and Mono for *nix).

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();

// To avoid deadlocks, always read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

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