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

标签 c# bash process terminal

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

一旦使用了“打开”命令,一旦终端或 iTerm 将在后台保持打开状态,此时调用带参数的“打开”命令将不再起作用。遗憾的是,我不得不手动退出应用程序以再次触发我的脚本。

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

我在网上搜索广告似乎无法解决,它已经花了很多时间来解决打开代码。非常感谢您的帮助。

这是我用来启动该过程的 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)

关于c# - 如何通过 System.Diagnostics.Process() 将参数传递给已经打开的终端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37628319/

相关文章:

bash - 不在 git 存储库中时无法抑制 git 分支错误

c# - 在 Visual Studio 中签署程序集时出现加密失败

c# - 有没有办法将代码镜像中编写的 css 应用于 iframe 的 html 内容?

c# - 如何对 PostSharp 方面进行单元测试?

c# - Nancy FX 在模型绑定(bind)上将字典中的键大写

linux kill命令-9 vs -15

bash - 如何激活 FULL $var 完成

linux - 未找到 Ansible 内部脚本命令

java - 该进程无法访问该文件,因为该文件正在被另一个进程使用

java - 如何实时获取流程构建器输出文本日志