我想运行以下代码。向stdin发送命令并发送发送结束(Ctrl + D)
我打电话的应用程序期望命令如User-Name = albert next line,另一个命令等等,直到完成为止。
如果您要从命令行手动输入此命令,请键入command,按Enter,键入命令按enter键。在最后一个命令后按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);
我在原始代码中有\ x4,对不起,这是我创建帖子时的错字。 Il尝试在4之前添加0并回报!谢谢,阿尔贝 – Albert 2010-03-03 22:53:25
嘿所有,我只是更新上面,请看看... – Albert 2010-03-04 20:01:32
想知道如果它的编码问题... ... – Albert 2010-03-04 20:14:25