我一直在忙于通过 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/