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

How to pass arguments to an already open terminal via System.Diagnostics.Process()

我一直在忙于通过 C# 触发 bash 脚本。当我第一次使用参数调用 "open" 命令时,这一切工作正常,这又通过终端打开我的 .command 脚本。

一旦使用 "open" 命令,一旦终端或 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

这可能是您要查找的内容:

Gets a stream used to write the input of the application.

MSDN | Process.StandardInput Property

// This could do the trick
process.StandardInput.WriteLine("..");