Click here to Skip to main content
15,867,686 members
Home / Discussions / C#
   

C#

 
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 
GeneralRe: Send/Receive text to CMD from C# GUI Pin
jschell15-Feb-13 8:57
jschell15-Feb-13 8:57 
Questionobject recognition Pin
manoj dhunelg14-Feb-13 2:42
manoj dhunelg14-Feb-13 2:42 
AnswerRe: object recognition Pin
Richard MacCutchan14-Feb-13 3:41
mveRichard MacCutchan14-Feb-13 3:41 
AnswerRe: object recognition Pin
Dave Kreskowiak14-Feb-13 6:14
mveDave Kreskowiak14-Feb-13 6:14 
Questionforcing users to use English language Pin
sina rafati nia13-Feb-13 21:23
sina rafati nia13-Feb-13 21:23 
AnswerRe: forcing users to use English language Pin
Manfred Rudolf Bihy13-Feb-13 22:29
professionalManfred Rudolf Bihy13-Feb-13 22:29 
AnswerRe: forcing users to use English language Pin
N a v a n e e t h13-Feb-13 22:30
N a v a n e e t h13-Feb-13 22:30 
GeneralRe: forcing users to use English language Pin
harold aptroot13-Feb-13 23:33
harold aptroot13-Feb-13 23:33 
AnswerRe: forcing users to use English language Pin
ali_heidari_14-Feb-13 5:55
ali_heidari_14-Feb-13 5:55 
AnswerRe: forcing users to use English language Pin
jschell14-Feb-13 11:46
jschell14-Feb-13 11:46 
Questionwebbrowser control in c# Pin
eldhose198513-Feb-13 18:28
eldhose198513-Feb-13 18:28 
AnswerRe: webbrowser control in c# Pin
N a v a n e e t h13-Feb-13 19:07
N a v a n e e t h13-Feb-13 19:07 
QuestionGridView FindControl LINQ Update Pin
RickSharp13-Feb-13 12:34
RickSharp13-Feb-13 12:34 
AnswerRe: GridView FindControl LINQ Update Pin
RickSharp13-Feb-13 12:43
RickSharp13-Feb-13 12:43 
AnswerRe: GridView FindControl LINQ Update Pin
Richard MacCutchan13-Feb-13 22:29
mveRichard MacCutchan13-Feb-13 22:29 

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.