CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2013
    Posts
    3

    Retrieve information pro command prompt

    I have this block of code, which I'm using to enter a command and retrieve the result:

    Code:
    private void Cmd(string command, string parameter, object stream)
            {
    
                StreamWriter writer = (StreamWriter)stream;
                StreamWriter input;
                StreamReader output;
    
                Process process = new Process();
    
                try
                {
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardInput = true;
                    process.StartInfo.FileName = "cmd";
                    process.Start();
    
                    input = process.StandardInput;
                    output = process.StandardOutput;
                    input.WriteLine("help");
    
                    while(!output.EndOfStream) //Locks when reach end of stream
                    {
                        writer.WriteLine(output.ReadLine());
                        Debug.WriteLine(output.ReadLine());
                    }
                    
                }
                catch (Exception e)
                {
                    InsertLog(2, "Could not run CMD command");
                    writer.WriteLine("Not valid. Ex: " + e.Message);
                }
    
                writer.Flush();
            }
    It works, except that the application locks when I reach the line highlighted on the code. On debug the yellow arrow simply disappear when reaches the while loop for the last time (end of stream).

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Retrieve information pro command prompt

    Not sure but it sounds like it is waiting on input again. So your help text was displayed and now it is looking for maybe the "Exit command?

    Try adding

    input.WriteLine("exit");

    After your loop.

  3. #3
    Join Date
    Jan 2013
    Posts
    3

    Re: Retrieve information pro command prompt

    Didn't work. Even tried to add WaitForExit(). Had to add it before the loop, now it works. Thank you.
    Last edited by blacblu; January 30th, 2013 at 02:18 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured