将密码流传递给流程标准输入 (C#)

问题描述

class MyProcess : Process {
//this is where all the StartInfo parameters are set
}        
MyProcess connectProc = new MyProcess();
connectProc.Start();
        
        int BuffSize = 64 * 1024;
        Char[] x = new Char[BuffSize];
        int bytesRead = 0;
        System.Threading.Thread.Sleep(1000);
    //reads the initial text until the first question
        singleResponse = "";
        bytesRead = connectProc.StandardOutput.Read(x,BuffSize);
        singleResponse = String.Concat(singleResponse,String.Join("",x).Substring(0,bytesRead));
        
        //prepares the answer streamwriter
        connectProc.myStreamWriter = connectProc.StandardInput;
        
        //the answers list
        string[] answers = {"y","1","myusername","mypassword","y" };
        
        //cycles through the answers
        foreach (string ans in answers)
        {
           connectProc.myStreamWriter.WriteLine(ans);
           System.Threading.Thread.Sleep(1000);
           singleResponse = "";
           bytesRead = connectProc.StandardOutput.Read(x,BuffSize);
           singleResponse = String.Concat(singleResponse,bytesRead));
        } 

上面的代码向作为 System.Diagnostics.Process 启动的命令行实用程序发送答案。

一切正常,直到我通过密码。这就是程序无限期停止的地方——就像 StreamWriter WriteLine 拒绝工作:没有任何错误。 当我手动启动该实用程序时,密码不会在我键入时显示(显然)。我怀疑这可能是导致该实用程序无法“感知”密码流的传递的原因。 有什么想法吗?

(附带说明,该实用程序确实允许将答案作为管道文件传递,但 Process 不喜欢管道字符作为参数“

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)