通过C#console-app执行一些命令 [英] Execute a few commands via C# console-app

查看:52
本文介绍了通过C#console-app执行一些命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手(通常是开发人员).我需要用C#编写一个将通过控制台执行的方法:-安装纽曼;-执行邮递员运行.

I'm new to C# (and development in general). I need to write a method in C# that will execute following via console: - install Newman; - execute postman Run.

我创建了一个如下所示的方法(我尝试了2个选项:ReadToEnd和WaitForExit),但似乎停留在每个步骤中.

I've created a method as below (I tried 2 options: ReadToEnd and WaitForExit), but it seems to stuck at each of these steps.

任何帮助如何使它依次执行两个命令(第一个需要在第二个开始之前完成)并在第二个命令完全执行后退出吗?

Any help how to make this to execute both commands in sequence (1st needs to finish before 2nd start) and exit after 2nd command executed fully?

谢谢.

    public string Runner ()
    {
        string readOutput = null;
        var psiNpm = new ProcessStartInfo
        {
            FileName = "cmd",
            RedirectStandardOutput = true,
            RedirectStandardInput = true,
            UseShellExecute = false
        };
        var pNpmRun = Process.Start(psiNpm);
        pNpmRun.StandardInput.WriteLine($"npm install -g newman");
        pNpmRun.WaitForExit();
        //pNpmRun.StandardOutput.ReadToEnd();
        pNpmRun.StandardInput.WriteLine($"newman run " +
            $"\"C:\\Postman\\Test.postman.json\" " +
            $"--folder \"TestSearch\" " +
            $"--environment \"C:\\Postman\\postman_environment.json\" " +
            $"--disable-unicode");
        pNpmRun.StandardOutput.ReadToEnd();
        pNpmRun.WaitForExit();
        return readOutput = pNpmRun.StandardOutput.ReadLine();
    }

推荐答案

尝试将 exit 命令写入您的 cmd 输入流.

Try writing an exit command to your cmd input stream.

问题是,您发送的命令(npm,newman)正在执行,然后控制权返回到外壳程序(cmd),然后外壳程序正在等待发送更多用户输入.您需要通过发送退出"来告诉它退出.您应该只有1个WaitForExit调用.WaitForExit正在等待 cmd 进程退出,而不是等待发送给它的单个命令.

The issue, is the commands you sent (npm, newman) are executing, then control is returning to the shell (cmd), which is then waiting for more user input to be sent. You need to tell it to quit by sending "exit". You should only have 1 WaitForExit call. The WaitForExit is waiting for the cmd process to exit, not the individual commands you send to it.

接下来,您应该在进程退出后(在 WaitForExit 之后)移动 ReadToEnd .这是一个关于为什么的问题:从流程和waitforexit的std输出读取ReadToEnd

Next, you should move ReadToEnd after the process has exited (after WaitForExit). Here is a question concering why: ReadToEnd from std output of process and waitforexit

所以,像这样:

public string Runner ()
    {
        var psiNpm = new ProcessStartInfo
        {
            FileName = "cmd",
            RedirectStandardOutput = true,
            RedirectStandardInput = true,
            UseShellExecute = false
        };
        var pNpmRun = Process.Start(psiNpm);
        pNpmRun.StandardInput.WriteLine("npm install -g newman");
        pNpmRun.StandardInput.WriteLine("newman run " +
            "\"C:\\Postman\\Test.postman.json\" " +
            "--folder \"TestSearch\" " +
            "--environment \"C:\\Postman\\postman_environment.json\" " +
            "--disable-unicode");
        pNpmRun.StandardInput.WriteLine("exit");

        pNpmRun.WaitForExit();
        return pNpmRun.StandardOutput.ReadToEnd();

您还可以考虑将超时添加到WaitForExit.如果这些cmds由于某种原因而被卡住,则您的调用过程也将被卡住,以等待它们完成.通常,最终失败比一堆挂起的进程更好.如果超时到期,则可以终止该进程.

You might also consider adding a timeout to the WaitForExit. If these cmds get stuck for some reason your calling process is also going to get stuck waiting for them to complete. Usually best to eventually fail than have a bunch of hung processes. You could then kill the process if the timeout expires.

  var tenMin = 10 * 60 * 1000;
  if(pNpmRun.WaitForExit(tenMin)) {
    return pNpmRun.StandardOutput.ReadToEnd();
  } else {
    pNpmRun.Kill();
    throw new TimeoutException("Command didn't complete in 10 minute timeout");
  }

这篇关于通过C#console-app执行一些命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆