System.Diagnostics.Process.Start()参数dotnet和diff [英] System.Diagnostics.Process.Start() arguments dotnet and diff

查看:74
本文介绍了System.Diagnostics.Process.Start()参数dotnet和diff的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试处理diff命令会给我一个错误:

Trying to process a diff command gives me an error:

diff: extra operand `>'

无论平台如何,错误都是相同的(在Windows下,我使用的是choco diffutils).

Error is the same regardless of platform (under windows, I'm using choco diffutils).

var cmd = "diff" //if ran under windows is the choco path: C:\\ProgramData\\chocolatey\\bin\\diff.exe
var args = "--unchanged-group-format='' --old-group-format='' --changed-group-format='%>' --new-group-format='' old.txt new.txt > diff.txt"
var p = System.Diagnostics.Process.Start(cmd, args)
p.WaitForExit()

推荐答案

之所以会发生这种情况,是因为>不是命令参数的一部分,而是标准输出重定向操作数,它不是由进程本身处理的,而是由操作系统启动的,过程.

This happens because > is not a part of the command arguments but the standard output redirection operand which is handled not by the process itself, but by the OS starting the process.

通过代码启动流程时,我们需要自己处理.

When starting a process through code, we need to handle this by ourselves.

以下是在Windows上运行的解决方案:

Here is a solution working on windows:

var cmd = "diff"; //if ran under windows is the choco path: C:\\ProgramData\\chocolatey\\bin\\diff.exe
var args = "--unchanged-group-format=\"\" --old-group-format=\"\" --changed-group-format=\"%>\" --new-group-format=\"\" old.txt new.txt";

var p = new Process();

p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = cmd;
p.StartInfo.Arguments = args;
p.StartInfo.RedirectStandardOutput = true;

p.Start();

using (var outputFile = File.OpenWrite("diff.txt"))
{
    p.StandardOutput.BaseStream.CopyTo(outputFile);
}

p.WaitForExit();

具有这两个文件(old.txt和new.txt)

Having these two files (old.txt and new.txt)

   old.txt             new.txt
Line 1 - abc        Line 1 - def
Line 2 - def        Line 2 - def
Line 1 - abc        Line 1 - def
Line 2 - def        Line 2 - def

输出(diff.txt)如下:

The output (diff.txt) is as follows:

Line 1 - def
Line 1 - def
Line 1 - def
Line 1 - def

这篇关于System.Diagnostics.Process.Start()参数dotnet和diff的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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