【问题标题】:Sending commands to stdin and sending end of transmit (Ctrl+D)向标准输入发送命令并发送传输结束 (Ctrl+D)
【发布时间】:2010-03-05 04:01:02
【问题描述】:

我想运行以下代码。

我正在调用的应用程序需要像 User-Name=albert 下一行这样的命令,另一个命令等等,直到你完成为止。

如果您要从命令行手动输入,您将键入命令,按回车,键入命令按回车。在最后一个命令后按 ENTER 后,您将按 Ctrl + D 结束它,这将运行程序并返回说明发生的情况。

如果您键入错误的命令,应用程序会显示“期望 =”,这意味着您发送的行格式不正确...

所以我在下面做的是模拟手动输入,一个命令,新行,一个命令,新行等,直到最后,然后我只是在最后一个命令之后附加 Ctrl+D。这不起作用,程序说“期待=”

有什么明显的问题吗?

感谢您的宝贵时间。 阿尔伯特

        // CREATE DISCONNECT FILE
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("User-Name=" + this.userName);
        sb.AppendLine("Acct-Session-Id=" + this.sessionID);
        sb.AppendLine("NAS-IP-Address=" + Setting.Item["EDGE.NASIP"]);
        sb.AppendLine("Framed-IP-Address=" + this.currentIP);
        sb.Append("/x4");
        System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo();
        procStartInfo.WorkingDirectory = radclientPath.Substring(0, radclientPath.LastIndexOf("\\"));
        procStartInfo.FileName = radclientPath;
        procStartInfo.Arguments = @"-r 1 -d ..\etc\raddb -x 192.168.1.240:3799 disconnect password";
        procStartInfo.RedirectStandardInput = true;
        procStartInfo.RedirectStandardError = true;
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;

        System.Diagnostics.Process proc = System.Diagnostics.Process.Start(procStartInfo);

        proc.StandardInput.Write(sb.ToString());
        proc.WaitForExit(5000);

        if (proc.HasExited)
        {
            System.IO.File.WriteAllText(@"D:\temp.txt", proc.StandardOutput.ReadToEnd());
            System.IO.File.WriteAllText(@"D:\temp2.txt", proc.StandardError.ReadToEnd());
            string message = proc.StandardOutput.ReadToEnd() + "\n---\nErrors:\n---\n" + proc.StandardError.ReadToEnd();
            return message;
        }
        System.IO.File.WriteAllText(@"D:\temp.txt", proc.StandardOutput.ReadToEnd());
        System.IO.File.WriteAllText(@"D:\temp2.txt", proc.StandardError.ReadToEnd());
        return "";
    }

我也在单引号中尝试了它,并且在数字 (\x04) 前加了一个零,也没有任何内容,所以我尝试将应该发送给程序的内容写入文本文件。 然后我使用如下相同的命令行参数运行程序。复制整个文本文件中的内容并将其粘贴到命令提示符中,效果很好。不知道为什么它没有正确发送到标准输入..

        StringBuilder sb = new StringBuilder();
        sb.AppendLine("User-Name=" + this.userName);
        sb.Append('\x04');
        System.IO.File.WriteAllText(@"D:\input.txt", sb.ToString());
        System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo();
        procStartInfo.WorkingDirectory = radclientPath.Substring(0, radclientPath.LastIndexOf("\\"));
        procStartInfo.FileName = radclientPath;
        procStartInfo.Arguments = @"-r 1 -d ..\etc\raddb -x 192.168.1.240:3799 disconnect password";
        procStartInfo.RedirectStandardInput = true;
        procStartInfo.RedirectStandardError = true;
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;
        System.Diagnostics.Process proc = System.Diagnostics.Process.Start(procStartInfo);
        proc.StandardInput.Write(sb.ToString());
        proc.WaitForExit(5000);

【问题讨论】:

    标签: c# command-line command-prompt


    【解决方案1】:

    您需要编写附加 "\x04",并带有一个反斜杠 (\)

    有关详细信息,请参阅documentation(字符串转义序列部分)

    【讨论】:

      【解决方案2】:
      sb.Append("/x4");
      

      这不是 control-D

      也许你的意思

      sb.Append("\x04");
      

      【讨论】:

      • 我的原始代码中有 \x4,抱歉,这是我创建帖子时的拼写错误。我尝试在 4 之前添加 0 并报告!谢谢,阿尔伯特
      猜你喜欢
      • 2017-09-07
      • 1970-01-01
      • 2012-06-26
      • 2022-08-14
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多