Unable to Read the total Output from the Command Prompt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sdanda
    New Member
    • Oct 2007
    • 36

    Unable to Read the total Output from the Command Prompt

    Hi,
    I am working on C# windows applications.Wh at i want to do is, I want to Redirect the output from Command Prompt to the Rich Text Box, Actually i am unable to read the entire output of the comman prompt, but i can see only the few lines.I don't know what's the wrong in my code.Here is the sample code.
    ChildLaunch()
    {
    Process p = new Process();
    p.StartInfo.Wor kingDirectory = RootDir;
    p.StartInfo.Fil eName = Environment.Get EnvironmentVari able("COMSPEC") ;
    p.StartInfo.Arg uments = "/c" + " " + CommandLine.Tri m();
    p.OutputDataRec eived += new DataReceivedEve ntHandler(p_Out putDataReceived );
    p.StartInfo.Red irectStandardOu tput = true;
    p.StartInfo.Red irectStandardIn put = true;
    p.StartInfo.Red irectStandardEr ror = true;
    p.StartInfo.Cre ateNoWindow = true;
    p.StartInfo.Use ShellExecute = false;
    p.EnableRaising Events = true;
    p.Start();
    p.BeginOutputRe adLine();
    }
    void p_OutputDataRec eived(object sender, DataReceivedEve ntArgs e)
    {
    richTextBox1.Ap pendText(e.Data );
    }


    Here RootDir is the working directory which we have to set in the command prompt. CommandLine is the String which i used in my code to collect all the elements from the GUI.
    Using my code I can read only some amount of output.. But i need to read the entire output. Can any one help me?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sdanda
    Hi,
    I am working on C# windows applications.Wh at i want to do is, I want to Redirect the output from Command Prompt to the Rich Text Box, Actually i am unable to read the entire output of the comman prompt, but i can see only the few lines.I don't know what's the wrong in my code.Here is the sample code.
    ChildLaunch()
    {
    Process p = new Process();
    p.StartInfo.Wor kingDirectory = RootDir;
    p.StartInfo.Fil eName = Environment.Get EnvironmentVari able("COMSPEC") ;
    p.StartInfo.Arg uments = "/c" + " " + CommandLine.Tri m();
    p.OutputDataRec eived += new DataReceivedEve ntHandler(p_Out putDataReceived );
    p.StartInfo.Red irectStandardOu tput = true;
    p.StartInfo.Red irectStandardIn put = true;
    p.StartInfo.Red irectStandardEr ror = true;
    p.StartInfo.Cre ateNoWindow = true;
    p.StartInfo.Use ShellExecute = false;
    p.EnableRaising Events = true;
    p.Start();
    p.BeginOutputRe adLine();
    }
    void p_OutputDataRec eived(object sender, DataReceivedEve ntArgs e)
    {
    richTextBox1.Ap pendText(e.Data );
    }


    Here RootDir is the working directory which we have to set in the command prompt. CommandLine is the String which i used in my code to collect all the elements from the GUI.
    Using my code I can read only some amount of output.. But i need to read the entire output. Can any one help me?
    After p.Start(), do
    [CODE=cpp]String output = p.StandardOutpu t.ReadToEnd();[/CODE]

    You can then send that output wherever you want.

    Comment

    • sdanda
      New Member
      • Oct 2007
      • 36

      #3
      Hi,
      I already tried by placing that statement but even after giving that statement i am unable to read the complete output

      The sample code with the above statement is
      delegate declaration:-
      delegate void WriteToRichText Box(string Msg);

      Thread declaration:-
      Thread t=new Thread(new ThreadStart(Chi ldLaunch);
      t.Start();


      void ChildLauch()
      {
      Process p = new Process();
      p.StartInfo.Wor kingDirectory = RootDir;
      p.StartInfo.Fil eName = Environment.Get EnvironmentVari able("COMSPEC") ;
      p.StartInfo.Arg uments = "/c" + " " + CommandLine.Tri m();
      p.OutputDataRec eived += new DataReceivedEve ntHandler(p_Out putDataReceived );
      p.StartInfo.Red irectStandardOu tput = true;
      p.StartInfo.Red irectStandardIn put = true;
      p.StartInfo.Red irectStandardEr ror = true;
      p.StartInfo.Cre ateNoWindow = true;
      p.StartInfo.Use ShellExecute = false;
      p.EnableRaising Events = true;
      p.Start();
      richTextBox1.In voke(new WriteToRichText Box(WriteText), new object[] {p.StandardOutp ut.ReadToEnd(). ToString()});
      p.Close();
      }

      public void WriteText(strin g msg)
      {
      richTextBox1.Ap pendText(msg);
      }

      Here even if i use synchronous ReadToEnd() function i am unable to see the total Command Line Output

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by sdanda
        Hi,
        I already tried by placing that statement but even after giving that statement i am unable to read the complete output

        The sample code with the above statement is
        delegate declaration:-
        delegate void WriteToRichText Box(string Msg);

        Thread declaration:-
        Thread t=new Thread(new ThreadStart(Chi ldLaunch);
        t.Start();


        void ChildLauch()
        {
        Process p = new Process();
        p.StartInfo.Wor kingDirectory = RootDir;
        p.StartInfo.Fil eName = Environment.Get EnvironmentVari able("COMSPEC") ;
        p.StartInfo.Arg uments = "/c" + " " + CommandLine.Tri m();
        p.OutputDataRec eived += new DataReceivedEve ntHandler(p_Out putDataReceived );
        p.StartInfo.Red irectStandardOu tput = true;
        p.StartInfo.Red irectStandardIn put = true;
        p.StartInfo.Red irectStandardEr ror = true;
        p.StartInfo.Cre ateNoWindow = true;
        p.StartInfo.Use ShellExecute = false;
        p.EnableRaising Events = true;
        p.Start();
        richTextBox1.In voke(new WriteToRichText Box(WriteText), new object[] {p.StandardOutp ut.ReadToEnd(). ToString()});
        p.Close();
        }

        public void WriteText(strin g msg)
        {
        richTextBox1.Ap pendText(msg);
        }

        Here even if i use synchronous ReadToEnd() function i am unable to see the total Command Line Output
        What if you use
        p.WaitForExit(W ithAMillseconds TimeOut); ?

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Have you checked to see if there is output on the Standard Error stream?

          Comment

          Working...