C#与CLI进程交互 - StandardInput停止执行

时间:2017-01-25 14:16:05

标签: c# asp.net .net command-line command-line-interface

我正在尝试使用C#StandardInputs和StandardOutputs与CLI进行交互。

我在process.StandardInput.WriteLine()之前和之后以及在StandardOutput循环之后插入了断点。

当我进入process.StandardInput.WriteLine()时,代码会立即停止执行并将焦点返回到我的主窗体(winform)。从不执行process.StandardInput.WriteLine()之后的断点(或任何其他代码)。

在循环中调用最后一个process.StandardOutput.WriteLine()时会发生同样的事情

        Process process = new Process();
        process.StartInfo.FileName = @"C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe";
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        process.Start();

        process.StandardInput.AutoFlush = true;
        process.StandardInput.WriteLine("connect vpn.service.domain.com");
        ...Additional code below

如果我在aprocess.StandardInput.WriteLine()之前使用此循环读取StandardOutput,则会发生同样的事情:

        string line = string.Empty;
        while ((line = process.StandardOutput.ReadLine()) != null)
        {
            Console.WriteLine("CLI Says: " + line);
        }
        ...Additional code below

我还注意到,如果我使用普通的GUI命令提示符运行Cisco VPN CLI,我会收到此输出:

  Copyright (c) 2004 - 2015 Cisco Systems, Inc.  All Rights Reserved.


  >> state: Disconnected
  >> state: Disconnected
  >> notice: Ready to connect.
  >> registered with local VPN subsystem.
  VPN>

但是当我运行process.StandardOutput.ReadLine()循环时我得到了这个输出。您可以看到所有额外的空白行和额外的“ VPN> ”提示:

Cisco AnyConnect Secure Mobility Client (version 3.1.10010) .


Copyright (c) 2004 - 2015 Cisco Systems, Inc.  All Rights Reserved.




  >> state: Disconnected


VPN> 
  >> state: Disconnected


VPN> 
  >> notice: Ready to connect.


VPN> 
  >> registered with local VPN subsystem.

1 个答案:

答案 0 :(得分:2)

process.StartInfo.FileName = @"C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe"; process.StartInfo.Arguments = "connect vpn.service.domain.com -s"; ... process.StandardInput.WriteLine(userDetails.UserName); process.StandardInput.WriteLine(userDetails.Password);

我添加-s并且它对我有用(这会将stdin扩展到控制台)。