如何从C#执行cygwin命令并将其保持打开状态?

时间:2017-09-24 07:15:56

标签: c# cygwin windows-console

我正在尝试通过C#中的bash运行cygwin命令,并尝试使用read命令保持结果打开。我的代码似乎打开了bash然后立即关闭。我做错了什么?

static void Main(string[] args)
{
    Process bashProcess = new Process();
    bashProcess.StartInfo.FileName = @"C:\cygwin64\bin\bash.exe";
    bashProcess.StartInfo.Arguments = "-l -c echo blah; read";
    bashProcess.StartInfo.UseShellExecute = true;
    bashProcess.Start();


    bashProcess.WaitForExit();
    System.Console.WriteLine("Exit Code: {0}", bashProcess.ExitCode);

    System.Console.WriteLine("Press enter to exit...");
    System.Console.ReadLine();
}

1 个答案:

答案 0 :(得分:2)

刚刚找到解决方案。用单引号括起整个命令使其工作。所以

bashProcess.StartInfo.Arguments = "-l -c echo blah; read";

应替换为以下内容:

bashProcess.StartInfo.Arguments = "-l -c 'echo blah; read'";