Click here to Skip to main content
15,887,477 members
Home / Discussions / C#
   

C#

 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson14-Feb-13 5:56
adam.m.b.nelson14-Feb-13 5:56 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
Richard MacCutchan14-Feb-13 6:01
mveRichard MacCutchan14-Feb-13 6:01 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson14-Feb-13 6:57
adam.m.b.nelson14-Feb-13 6:57 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
Richard MacCutchan14-Feb-13 7:03
mveRichard MacCutchan14-Feb-13 7:03 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson14-Feb-13 7:45
adam.m.b.nelson14-Feb-13 7:45 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson14-Feb-13 8:02
adam.m.b.nelson14-Feb-13 8:02 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
Richard MacCutchan14-Feb-13 6:29
mveRichard MacCutchan14-Feb-13 6:29 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson14-Feb-13 7:04
adam.m.b.nelson14-Feb-13 7:04 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
Richard MacCutchan14-Feb-13 7:21
mveRichard MacCutchan14-Feb-13 7:21 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson14-Feb-13 8:06
adam.m.b.nelson14-Feb-13 8:06 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
Richard MacCutchan14-Feb-13 9:41
mveRichard MacCutchan14-Feb-13 9:41 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson14-Feb-13 9:48
adam.m.b.nelson14-Feb-13 9:48 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
Richard MacCutchan14-Feb-13 10:08
mveRichard MacCutchan14-Feb-13 10:08 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson14-Feb-13 10:27
adam.m.b.nelson14-Feb-13 10:27 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
Richard MacCutchan14-Feb-13 22:57
mveRichard MacCutchan14-Feb-13 22:57 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson15-Feb-13 2:21
adam.m.b.nelson15-Feb-13 2:21 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson3-Feb-15 6:11
adam.m.b.nelson3-Feb-15 6:11 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
Richard MacCutchan14-Feb-13 6:35
mveRichard MacCutchan14-Feb-13 6:35 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson14-Feb-13 6:54
adam.m.b.nelson14-Feb-13 6:54 
AnswerRe: Send/Receive text to CMD from C# GUI Pin
Alan N14-Feb-13 7:32
Alan N14-Feb-13 7:32 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson14-Feb-13 7:48
adam.m.b.nelson14-Feb-13 7:48 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson14-Feb-13 8:23
adam.m.b.nelson14-Feb-13 8:23 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
Alan N14-Feb-13 9:57
Alan N14-Feb-13 9:57 
OK here's the improved version of your program (it's a console program)

I'll list what I've done:
1) Fix error: you had used Process.Start twice
2) Assigned a handler for the OutputDataReceived event. Redirected output is prefixed REDIR to differentiate from normal program output.
3) Activated Exited event and and assigned a handler
4) Error not redirected (makes the test simpler)
5) Start reading redirected standard output after Process.Start()
6) Issue DIR command
7) Issue SET command (to dump the environment variables)
8) Issue EXIT command to close cmd.exe
[EDIT forgot this]
9) Don't close the Process object

C#
using System;
using System.Diagnostics;
namespace AdamB {
  class Program {
    static void Main(string[] args) {
      Console.WriteLine("AdamB Test app");
      RedirectionTest();
      Console.WriteLine("Press Enter to end when output is complete...");
      Console.WriteLine();
      Console.ReadLine();
    }

    private static void RedirectionTest() {
      Process Temp = new Process();
      Temp.StartInfo.FileName = "cmd.exe";
      Temp.StartInfo.UseShellExecute = false;
      Temp.StartInfo.RedirectStandardInput = true;

      // Want redirected output
      Temp.StartInfo.RedirectStandardOutput = true;
      Temp.OutputDataReceived += new DataReceivedEventHandler(Temp_OutputDataReceived);

      // Want Exit event
      Temp.EnableRaisingEvents = true;
      Temp.Exited += new EventHandler(Temp_Exited);

      Temp.Start();

      // Start reading redirected output
      Temp.BeginOutputReadLine();

      // Send commands
      Console.WriteLine("Sending DIR");
      Temp.StandardInput.WriteLine("DIR");
      Console.WriteLine("Sending SET");
      Temp.StandardInput.WriteLine("SET");
      Console.WriteLine("Sending EXIT");
      Temp.StandardInput.WriteLine("EXIT");
    }

    static void Temp_Exited(object sender, EventArgs e) {
      Console.WriteLine("MESSAGE: The process has exited");
    }

    static void Temp_OutputDataReceived(object sender, DataReceivedEventArgs e) {
      Console.WriteLine("REDIR {0}", e.Data ?? "Null received -> Output stream has closed");
    }
  }
}


It's not usual for all commands to be issued before any redirected output appears on the screen and for the Exited event to occur while output is still arriving. Output has finished when the redirected stream is closed and that is signalled by e.Data == null.

Understanding all this redirection stuff is not easy if you are taking your cues from the Microsoft documentation as the examples are poor.

I'll look for more questions tomorrow.

Alan.

modified 14-Feb-13 16:40pm.

GeneralRe: Send/Receive text to CMD from C# GUI Pin
jschell14-Feb-13 11:44
jschell14-Feb-13 11:44 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
adam.m.b.nelson15-Feb-13 2:22
adam.m.b.nelson15-Feb-13 2:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.