2010-03-03 86 views
2

我想運行以下代碼。向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); 

回答

0
sb.Append("/x4"); 

這不是一個控制d

也許你的意思是

sb.Append("\x04"); 
+0

我在原始代碼中有\ x4,對不起,這是我創建帖子時的錯字。 Il嘗試在4之前添加0並回報!謝謝,阿爾貝 – Albert 2010-03-03 22:53:25

+0

嘿所有,我只是更新上面,請看看... – Albert 2010-03-04 20:01:32

+0

想知道如果它的編碼問題... ... – Albert 2010-03-04 20:14:25

2

你需要寫追加"\x04" ,用backslah(\

欲瞭解更多信息,請參閱documentation(字符串轉義序列部分)