微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决将密码流传递给流程标准输入 (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 举报,一经查实,本站将立刻删除。