簡體   English   中英

如何使用 C#/Mono 以編程方式在 mac 終端中執行命令

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM