Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要输入时从Process StandardOut读取提示_C#_.net_Stream - Fatal编程技术网

C# 需要输入时从Process StandardOut读取提示

C# 需要输入时从Process StandardOut读取提示,c#,.net,stream,C#,.net,Stream,我正在使用System.Diagnostics.Process创建新进程,并与操作系统通信以运行命令 该命令有时需要用户名、密码,两者都需要,什么都不需要 如果我从StandardOutput读取,它将立即退出。如果我只是盲目地提供密码\n当我知道它在测试场景中需要密码时,它可以工作,与第三方进行身份验证,并且我可以从StandardOutput读取结果 作为测试,我尝试了以下方法: while (_process.StandardOutput.Peek() > -

我正在使用System.Diagnostics.Process创建新进程,并与操作系统通信以运行命令

该命令有时需要用户名、密码,两者都需要,什么都不需要

如果我从StandardOutput读取,它将立即退出。如果我只是盲目地提供密码\n当我知道它在测试场景中需要密码时,它可以工作,与第三方进行身份验证,并且我可以从StandardOutput读取结果

作为测试,我尝试了以下方法:

            while (_process.StandardOutput.Peek() > -1)
            Log.Info((char)_process.StandardOutput.Read());
这将导致命令立即退出,并显示失败的退出代码。我尝试了各种各样的方法,但我无法找到一种方法来确定它是否需要用户名或密码,或者是否已经有了用户名或密码(它只是在其中输出结果集)

我需要做(伪代码)


我知道如果需要,命令将显示第一个单词的用户名或密码,然后等待我提供它。

以下是开始的基本步骤:

Process myProcess = new Process();
myProcess .StartInfo.FileName = "yourCommand.exe";
myProcess .StartInfo.Arguments = "-yourArguments";
myProcess .StartInfo.UseShellExecute = false;//do not open shell window
myProcess .StartInfo.RedirectStandardOutput = true; // allows you to manipulate or suppress the output of a process
myProcess.StartInfo.RedirectStandardInput = true;// allows you to manipulate or suppress the input of a process
myProcess .Start()
在此之后,youCommand.exe的输出非常可靠

如果yourCommand.exe在一行中请求用户,并且提示插入符号转到一个新行,如输入用户名:/r/n,则可以执行一个过程。StandardOutput.ReadLine();并解析该行以查看是请求用户还是另一条消息

如果没有新行,则必须处理一个
线程.sleep
和一个循环,以查看您的command.exe何时请求输入

例如,询问用户时有新行,询问密码时没有新行:

这是yourCommand.exe

class ConsoleApplication1
{
    void Main()
    {
        Console.WriteLine("User:");
        string user = Console.ReadLine();
        Console.Write("Password:");//no new line!!!
        string pass = Console.ReadLine();
        Console.WriteLine(string.Format("The user {0} has the password {1}", user, pass));
    }
}
这就是你想要做的:

namespace ConsoleApplication2
{
 class Program
 {
  static void Main(string[] args)
  {
   Process myProcess = new Process();
   myProcess.StartInfo.FileName = @"C:\Users\joseluis.vaquero\Documents\Visual Studio 2008\Projects\Prototipos\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe";
   // myProcess.StartInfo.Arguments = "-yourArguments";
   myProcess.StartInfo.UseShellExecute = false;//do not open shell window
   myProcess.StartInfo.RedirectStandardOutput = true; // allows you to manipulate or suppress the output of a process
   myProcess.StartInfo.RedirectStandardInput = true;// allows you to manipulate or suppress the input of a process
   myProcess.Start();
 //  Thread.Sleep(1000);//no need to wait, next ReadLine wait for you
   Console.WriteLine(myProcess.StandardOutput.ReadLine());
   myProcess.StandardInput.WriteLine("jlvaquero");//send user name to consoleapp1
   Thread.Sleep(1000);//need to wait consoleapp1 to ask for pwd because we can not use ReadLine
   char[] myBuffer = new char[1];
   while (myBuffer[0] != ':') //read until double dot wich is the last char of "Password:"
   {
     myProcess.StandardOutput.Read(myBuffer, 0, 1);
    Console.Write(myBuffer[0]);
   }
   Console.WriteLine();//just to get readable output for the example
   myProcess.StandardInput.WriteLine("1234");//send pwd to consoleapp1
   Console.Write(myProcess.StandardOutput.ReadLine());//show the output of consoleapp1
   Console.Read();
  }
 }
}

它没有新行,我尝试了peek and Read(),但它会导致进程突然退出。我不知道该怎么解决这个问题。它基本上是一种类似于。未提供用户名,请输入用户名:密码无效,请输入密码:@Michael。您可以添加用户名并传入二进制文件的参数吗?因为使用myProcess.StartInfo.Arguments=“-yourArguments”;也许您不需要交互进程。不幸的是,不需要。如果可以的话,当提供错误的凭据时,它仍然无法解决问题(因为这将再次强制提示并锁定调用它的应用程序)。但我想我可能知道更多的事情。该命令正在生成第二个进程,该进程正在处理与远程服务的通信,我认为输出可能从那里通过管道传输,但我没有该应用程序的句柄,因此无法看到它。我不确定是否有一个切实可行的方法来做这件事。好吧,根据你在问题中提供的信息和第二个过程的混乱,我想我不能做得更多了。我很抱歉。我感谢你的反馈,我认为第二个过程是造成所有悲伤的原因,我没有意识到最初是这样做的,我花了很多时间把头撞在墙上。我正在尝试使用本机DLL解决方案找到另一种解决方法。我认为你的解决方案很有帮助。
namespace ConsoleApplication2
{
 class Program
 {
  static void Main(string[] args)
  {
   Process myProcess = new Process();
   myProcess.StartInfo.FileName = @"C:\Users\joseluis.vaquero\Documents\Visual Studio 2008\Projects\Prototipos\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe";
   // myProcess.StartInfo.Arguments = "-yourArguments";
   myProcess.StartInfo.UseShellExecute = false;//do not open shell window
   myProcess.StartInfo.RedirectStandardOutput = true; // allows you to manipulate or suppress the output of a process
   myProcess.StartInfo.RedirectStandardInput = true;// allows you to manipulate or suppress the input of a process
   myProcess.Start();
 //  Thread.Sleep(1000);//no need to wait, next ReadLine wait for you
   Console.WriteLine(myProcess.StandardOutput.ReadLine());
   myProcess.StandardInput.WriteLine("jlvaquero");//send user name to consoleapp1
   Thread.Sleep(1000);//need to wait consoleapp1 to ask for pwd because we can not use ReadLine
   char[] myBuffer = new char[1];
   while (myBuffer[0] != ':') //read until double dot wich is the last char of "Password:"
   {
     myProcess.StandardOutput.Read(myBuffer, 0, 1);
    Console.Write(myBuffer[0]);
   }
   Console.WriteLine();//just to get readable output for the example
   myProcess.StandardInput.WriteLine("1234");//send pwd to consoleapp1
   Console.Write(myProcess.StandardOutput.ReadLine());//show the output of consoleapp1
   Console.Read();
  }
 }
}