【问题标题】:C# Mono Process input errorC# Mono 进程输入错误
【发布时间】:2016-07-04 22:35:37
【问题描述】:

您好 StackOverflow 用户。我正在尝试编写一些代码来打开一个新的控制台/终端窗口,启动 GnuPlot,执行gnuplot 命令,然后使用plot sin(x) 绘制正弦函数。

请注意,如果您在 gnuplot 命令中运行它,它只会打印出来。 GnuPlot 程序有点像 Python shell,如果您还没有了解的话。

问题

我可以让它执行gnuplot,很好,它在当前窗口上运行命令(不是这个问题的问题,但请随时解决!) 但它不将plot sin(x) 识别为命令。我的猜测是它会执行 gnuplot 并以某种方式退出 GnuPlot shell 并返回正常的控制台模式。

注意:我使用的是 KUbuntu 16.04 LTS。

代码

public void Plot() {
        // GetCommand() always returns "gnuplot" for now.

        ProcessStartInfo pInfo = new ProcessStartInfo("/bin/bash", String.Format("-c {0};plot sin(x)", GetCommand())) {
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            CreateNoWindow = true,
            UseShellExecute = false
        }; 

        Process process = new Process () { StartInfo = pInfo };

        process.Start ();
    }

控制台返回错误:sin(x): plot: command not found

我尝试过的

要将; plot sin(x) 替换为&& plot sin(x),将返回相同的错误。

要使用process.StandardInput.WriteLine ("plot sin(x)"); 行写入StandardInput,但是它会抛出一个System.InvalidOperationException 通知Standard input has not been redirected,即使我在ProcessStartInfo 上重定向它!

【问题讨论】:

    标签: c# shell process mono


    【解决方案1】:

    您可以使用fifo 文件作为gnuplot 的输入文件,然后写入该文件以提供gnuplot 脚本命令。

    (详情man mkfifo

    示例:

    Process.Start("mkfifo", "/tmp/plotpipe");
    var pInfo = new ProcessStartInfo(@"/usr/local/bin/gnuplot", "/tmp/plotpipe")
    {
        RedirectStandardInput = false,
        RedirectStandardOutput = false,
        CreateNoWindow = true,
        UseShellExecute = false
    };
    var process = new Process() { StartInfo = pInfo };
    process.Start();
    using (StreamWriter file = new StreamWriter(@"/tmp/plotpipe", false))
    {
        file.Write("print sum [i=1:10] i");
    }
    Process.Start("rm", "/tmp/plotpipe");
    

    输出:

    55.0
    
    Press any key to continue...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-17
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多