2016-07-21 13 views
0

cmdコマンドの結果をrichtextboxに表示する際に問題が発生しました。 "Console.WriteLineを(e.Data)を、" 私が交換しようとしたcmdコマンドを入力してrichtextboxに結果を表示するC#

private void richTextBox2_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Enter) 
     { 
      e.Handled = true; 
      e.SuppressKeyPress = true; 
       ProcessStartInfo cmdStartInfo = new ProcessStartInfo(); 
       cmdStartInfo.FileName = @"C:\Windows\System32\cmd.exe"; 
       cmdStartInfo.RedirectStandardOutput = true; 
       cmdStartInfo.RedirectStandardError = true; 
       cmdStartInfo.RedirectStandardInput = true; 
       cmdStartInfo.UseShellExecute = false; 
       cmdStartInfo.CreateNoWindow = true; 

       Process cmdProcess = new Process(); 
       cmdProcess.StartInfo = cmdStartInfo; 
       cmdProcess.OutputDataReceived += cmd_DataReceived; 
       cmdProcess.EnableRaisingEvents = true; 
       cmdProcess.Start(); 
       cmdProcess.BeginOutputReadLine(); 
       cmdProcess.BeginErrorReadLine(); 

       cmdProcess.StandardInput.WriteLine(richTextBox2.Text);  
       cmdProcess.StandardInput.WriteLine("exit");     

       cmdProcess.WaitForExit(); 

       richTextBox1.Text = richTextBox1.Text + cmd_DataReceived + Environment.NewLine; 

     } 
    } 

static void cmd_DataReceived(object sender, DataReceivedEventArgs e) 
    { 
     Console.WriteLine("Output from other process"); 
     Console.WriteLine(e.Data); 
    } 

: はこれまでのところ、私はこのコードを持っています"richTextBox1.Text = richTextBox1.Text +(e.Data);"それは受け入れられないだろう。 私も "richTextBox1.Text = richTextBox1.Text + cmd_DataReceived"を試しました。 しかし、それはietherでは動作しませんでした。 は、それから私はmessagebox.show(e.data)でConsole.WriteLineをを交換しようとしたが、再び...いや コマンドが動作しますが、それは私がコードのほとんどをコピーし、それはおそらく意味を知っている

は表示されませんコンソールアプリケーション用。あなたはcmdProcess.WaitForExitで終了するプロセスを待っているとして

+0

あなたがここでやろうとしているものは明らかではありません。 richtextboxの内容をInputStreamに書き込むので、richtextboxにcmd.exeの引数を書き込んでコマンドプロンプトに渡したいと思うようですが、結果をrichtextboxに表示するように質問します。ところで、CMD.EXEへの呼び出しでは、パラメータなしで出力はありません。 – Steve

+0

hmm。すべてのコマンドにすべてのコマンドプロンプトの回答を追加する場合を除き、これはほとんど不可能です。 はい、私はあなたがそれを正しく理解したと思います。 – KrisPus

+0

私は他の誰かが非常に似たようなことをするのを手伝っていました。あなたのコードはうまく見えますが、あなたの唯一の問題はイベントが自分のスレッドで実行されていて、どのスレッドからもUIを更新できないということです。UIスレッドからしか更新できません。ここで私の要素から少し外れていますが、InvokeまたはBeginInvokeを使用する必要があります。また、Dispatcher(例:[ここ])(http://stackoverflow.com/questions/4446048/how-to-update-textboxes-in -main-thread-from-another-thread)と[here](http://stackoverflow.com/questions/19009174/dispatcher-invoke-vs-begininvoke-confusion)を参照してください。 – Quantic

答えて

0

を助けてください、cmd_DataReceivedは、すべてのデータを取得しています。これは、コールバックから取得したデータを保存する必要があることを意味し、WaitForExitが終了した後、保存されたデータの内容でテキストボックスを設定できます。

一つの簡単な方法は、StringBuilderを使用することです:

// Have a reference to a stringbuilder that both functions can see 
    StringBuilder m_output; 

    private void richTextBox2_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Enter) 
     { 
      // Before the process starts, create a new stringbuilder 
      m_output = new StringBuilder(); 

      e.Handled = true; 
      e.SuppressKeyPress = true; 
      ProcessStartInfo cmdStartInfo = new ProcessStartInfo(); 
      cmdStartInfo.FileName = @"C:\Windows\System32\cmd.exe"; 
      cmdStartInfo.RedirectStandardOutput = true; 
      cmdStartInfo.RedirectStandardError = true; 
      cmdStartInfo.RedirectStandardInput = true; 
      cmdStartInfo.UseShellExecute = false; 
      cmdStartInfo.CreateNoWindow = true; 

      Process cmdProcess = new Process(); 
      cmdProcess.StartInfo = cmdStartInfo; 
      cmdProcess.OutputDataReceived += cmd_DataReceived; 
      cmdProcess.EnableRaisingEvents = true; 
      cmdProcess.Start(); 
      cmdProcess.BeginOutputReadLine(); 
      cmdProcess.BeginErrorReadLine(); 

      cmdProcess.StandardInput.WriteLine(richTextBox2.Text); 
      cmdProcess.StandardInput.WriteLine("exit"); 

      cmdProcess.WaitForExit(); 

      // And now that everything's done, just set the text 
      // to whatever's in the stringbuilder 
      richTextBox1.Text = m_output.ToString(); 

      // We're done with the stringbuilder, let the garbage 
      // collector free it 
      m_output = null; 
     } 
    } 

    // Note: This is no longer a static method so it has 
    // access to the member variables, including m_output 
    void cmd_DataReceived(object sender, DataReceivedEventArgs e) 
    { 
     Debug.WriteLine("Output from other process"); 
     Debug.WriteLine(e.Data); 

     // Add the data, one line at a time, to the string builder 
     m_output.AppendLine(e.Data); 
    } 
+0

それはうまくいった!さらに、「Microsoft Windows [バージョン6.1.7601] Copyright(c)2009 Microsoft Corporation。無断複写・転載を禁じます」テキストの前に、私はそれのためのフィルタ文字列を作ることができます:) – KrisPus

+0

私は文字列を追加しました:文字列RAW = m_output.ToString()を分割します(新しい文字列[] {">"}、StringSplitOptions.None)。 文字列RAW2 = RAW.Split(新しい文字列[] {richTextBox2.Text + "&exit"}、StringSplitOptions.RemoveEmptyEntries).Last(); しかし、多くの空白行が表示されましたが、問題ありません – KrisPus

関連する問題