[英]How can I launch pipes and redirects commands with Process.Start?
我正在使用System.Diagnostics.Process.Start()在Linux操作系统上远程启动命令。 到目前为止,我已经能够启动简单的命令,然后读取输出。
例如,我可以执行命令echo Hello World
并读取Hello World
作为其输出。
这是简化的代码:
public void Execute(string file, string args) {
Process process = new Process {
StartInfo = {
FileName = file,
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
}
};
process.Start();
}
为了更清楚,我使用上面的代码: Execute("echo", "Hello World");
。
这是我的问题:只要我执行简单的命令一切顺利,但我想用管道和重定向启动命令,以便对命令及其输出有更强的控制(无需将输出本身作为文本处理)。
那么,是否有解决方法(或者可能是特定的库)来实现这一结果?
为了在Linux中执行具有所有shell功能(包括管道,重定向等)的命令,请使用以下代码:
public static void ExecuteInBash(string command)
{
var process = new Process
{
StartInfo =
{
FileName = "bash",
Arguments = "-c \"" + command + "\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
}
};
process.Start();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.