如何使用C#/Mono在mac终端中以编程方式执行命令
我可以使用如何使用C#/Mono在mac终端中以编程方式执行命令,c#,mono,C#,Mono,我可以使用系统.Diagnostics.Process打开新的终端窗口,但无法使用标准输入.WriteLine向终端写入任何内容 终端窗口打开,但没有命令写入终端窗口 示例代码: var startInfo = new ProcessStartInfo { FileName = @"/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal", UseShellExecute =
系统.Diagnostics.Process
打开新的终端窗口,但无法使用标准输入.WriteLine
向终端写入任何内容
终端窗口打开,但没有命令写入终端窗口
示例代码:
var startInfo = new ProcessStartInfo {
FileName = @"/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UserName = System.Environment.UserName
};
using (var process = Process.Start (startInfo)) {
process.StandardInput.WriteLine ("nuget"); // cannot get anything written to the terminal
}
我认为您可以使用以下代码:
public static void ExecuteCommand(string command)
{
Process proc = new System.Diagnostics.Process ();
proc.StartInfo.FileName = @"/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal";
proc.StartInfo.Arguments = "-c \" " + command + " \"";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start ();
while (!proc.StandardOutput.EndOfStream) {
Console.WriteLine (proc.StandardOutput.ReadLine ());
}
}
public static void Main (string[] args)
{
ExecuteCommand("nuget");
}
谢谢你的回复,但在我这方面似乎没有达到预期效果。最初我确实尝试过
StartInfo.Arguments
,但没有任何东西可以输入到终端。@SamuelG然后你可以删除该参数,然后再试一次,它完全适用于Ubuntu。是的,我尝试过参数和无参数组合,以及StandardInput.WriteLine
。在mac上没有得到结果