如何在 C# Mono 中执行 Linux 命令行 [英] How to execute Linux command line in C# Mono

查看:147
本文介绍了如何在 C# Mono 中执行 Linux 命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行这个命令: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";进程 p = Process.Start(psi);p.WaitForExit();p.Close();

解决方案

你只能使用<>|壳内的运算符.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;进程 p = Process.Start(psi);Console.WriteLine(p.StandardOutput.ReadToEnd());p.WaitForExit();p.Close();...

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

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

祝你好运!

I want to execute this command : iconv -f unicode -t utf8 input.txt > output.txt

But I got this error : /usr/bin/iconv: cannot open input file `>': No such file or directory

            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();

解决方案

You can only use the <, > and | operators inside a shell. The shell (such as bash) is what actually parses these and performs the redirection. Try this code:

...
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();
...

I apologize if this code doesn't work properly, I personally rarely program in C#.

You may also be able to just set psi.UseShellExecute = true. According to MSDN, this will start the program with the system shell (cmd.exe). This may work for you, although I have not tested.

Best of luck!

这篇关于如何在 C# Mono 中执行 Linux 命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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