【问题标题】:How to execute Linux command line in C# Mono如何在 C# Mono 中执行 Linux 命令行
【发布时间】:2021-02-05 12:08:48
【问题描述】:

我要执行这个命令:iconv -f unicode -t utf8 input.txt > output.txt

但是我得到了这个错误:/usr/bin/iconv: 无法打开输入文件`>': 没有这样的文件或目录

            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = "/usr/bin/iconv";
            psi.UseShellExecute = false;
            psi.Arguments = "-f unicode -t utf8 /tmp/test.txt > Desktop/output.txt";
            Process p = Process.Start(psi);
            p.WaitForExit();
            p.Close();

【问题讨论】:

  • 你为什么将它设置为重定向标准输出,并且还试图将输出发送到文本文件?
  • 只是我在闲散一个例子,但我不使用它

标签: c# linux mono


【解决方案1】:

您只能在 shell 中使用 <>| 运算符。 shell(例如 bash)实际上是解析这些并执行重定向的。试试这个代码:

...
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "/usr/bin/iconv";
psi.UseShellExecute = false;
psi.Arguments = "-f unicode -t utf8 /tmp/test.txt";
psi.RedirectStandardOutput = true;
Process p = Process.Start(psi);
Console.WriteLine(p.StandardOutput.ReadToEnd());
p.WaitForExit();
p.Close();
...

如果这段代码不能正常工作,我深表歉意,我个人很少用 C# 编程。

您也可以只设置psi.UseShellExecute = true。根据 MSDN,这将使用系统 shell (cmd.exe) 启动程序。这可能对你有用,虽然我没有测试过。

祝你好运!

【讨论】:

  • 工作就像一个魅力!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
相关资源
最近更新 更多