[英]How to programmatically execute commands in mac terminal using C#/Mono
我可以使用System.Diagnostics.Process
打開一個新的終端窗口,但不能使用StandardInput.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");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.