微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CodeGo.net>如何通过System.Diagnostics.Process()将参数传递给已经打开的终端

我一直在通过C#触发bash脚本.当我第一次使用参数调用“ open”命令时,所有这些都可以正常工作,而后者又通过Terminal打开.command脚本.

一旦使用了“ open”命令,Terminal或iTerm将在后台保持打开状态,此时调用带有参数的“ open”命令将不再起作用.可悲的是,我必须手动退出该应用程序才能再次触发我的脚本.

如何在不退出的情况下将参数传递给已经打开的终端应用程序以重新启动脚本?

搜索的在线广告似乎无法解决,解决开放代码已经花费了很多时间.非常感谢您的帮助.

这是我用来启动该过程的C#代码

var p = new System.Diagnostics.Process();
    p.StartInfo.FileName = "open";
    p.StartInfo.WorkingDirectory = installFolder;
    p.StartInfo.Arguments = "/bin/bash --args \"open \"SomePath/Commands/myscript.command\"\"";
    p.Start();

谢谢

编辑:
这两个答案都是正确的,这可能对其他人有帮助:

    processstartinfo startInfo = new processstartinfo("/bin/bash");
    startInfo.WorkingDirectory = installFolder;
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardInput = true;
    startInfo.RedirectStandardOutput = true;

    Process process = new Process();
    process.StartInfo = startInfo;
    process.Start();

    process.StandardInput.WriteLine("echo helloworld");
    process.StandardInput.WriteLine("exit");  // if no exit then WaitForExit will lockup your program
    process.StandardInput.Flush();

    string line = process.StandardOutput.ReadLine();

    while (line != null)
    {
        Debug.Log("line:" + line);
        line = process.StandardOutput.ReadLine();
    }
    process.WaitForExit();
    //process.Kill(); // already killed my console told me with an error

解决方法:

你可以试试:

调用p.Start()之前:

p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
// for the process to take commands from you, not from the keyboard

之后:

if (p != null)
{
    p.StandardInput.WriteLine("echo helloworld");
    p.StandardInput.WriteLine("executable.exe arg1 arg2");
}

(取自here)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐