2011-06-20 3 views
2

私は中国出身ですから、英語が苦手なのかもしれません。私はあなたが私の質問を理解できるように最善を尽くしています。 C#プロジェクトでPHP CLIを使いたいです。 私が返された文字列として、このC#でPHP CLIを使うには

 Process p = new Process(); 
     p.StartInfo.FileName = "cmd.exe"; 
     p.StartInfo.Arguments = command; 
     p.StartInfo.UseShellExecute = false; 
     p.StartInfo.RedirectStandardInput = true; 
     p.StartInfo.RedirectStandardOutput = true; 
     p.StartInfo.RedirectStandardError = true; 
     p.StartInfo.CreateNoWindow = true; 
     try 
     { 
      p.Start(); 
      p.StandardInput.WriteLine(command); 
      p.StandardInput.WriteLine("exit"); 
      p.WaitForExit(1000); 
      StreamReader reader = new StreamReader(p.StandardOutput.BaseStream, Encoding.GetEncoding("utf-8")); 
      string text= reader.ReadToEnd(); 
      if (text.IndexOf(command) != -1) 
      { 
       int start = text.IndexOf(command) + command.Length; 
       string endstring = rootpath + ">exit"; 
       int end = text.IndexOf(endstring); 
       text = text.Substring(start, text.Length - start - endstring.Length).Trim(); 

       return text; 
      } 
      return ""; 
     } 
     catch (System.Exception ex) 
     { 
      return ""; 
     } 
     finally 
     { 
      p.Close(); 
     } 

のようなコードを試してみましたが、私は正しい結果を得るために、部分文字列を使用しますが、時々私は私が真実でに欲しいものを得ることができない、私は必要なものではありません。 私の方法は正しいとは思わないかもしれませんが、私はインターネットからの情報を見つけることができません。

私を助けることができますか?どうもありがとうございました!あなたの返信をお待ちしております。

+1

質問を書式設定し、コードタグでコードを囲みます。 – Roman

+3

実際の結果がどのようなものか、予想される結果がどのようなものか説明してください。 – Oded

答えて

1

私があなたのコード例を見ると、質問は意味をなさない。しかし、あなたの質問に応じて、あなたは、CLIからPHPスクリプトを実行すると、以下のようなものを使用して出力を収集することができます。このコードの

System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
string sOutput = ""; 

proc.EnableRaisingEvents = false; 
proc.StartInfo.FileName = "php.exe"; 
proc.StartInfo.Arguments = "-f file.php"; 
proc.StartInfo.RedirectStandardOutput = true; 

System.IO.StreamReader hOutput = proc.StandardOutput; 

proc.WaitForExit(2000); 

if(proc.HasExited) 
    sOutput = hOutput.ReadToEnd();   

半分は自分であると私は私がGoogle経由で見つけsnippet由来残り。

お探しのとおりです。

+0

はい、ありがとうあなたの助けを非常に、私はこの問題をあなたのおかげで同様の方法を使用して解決しました –

関連する問題