2

我想运行以下代码。

我正在调用的应用程序需要像 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);
4

2 回答 2

2

您需要"\x04"使用反斜杠 ( \)编写 append

有关更多信息,请参阅文档 (字符串转义序列部分)

于 2010-03-03T20:31:27.707 回答
0
sb.Append("/x4");

这不是控制-D

也许你的意思是

sb.Append("\x04");
于 2010-03-03T20:24:40.907 回答