How to execute Linux command line in C# Mono

泄露秘密 提交于 2021-01-29 02:10:25

问题


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

回答1:


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!



来源:https://stackoverflow.com/questions/33553862/how-to-execute-linux-command-line-in-c-sharp-mono

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!