Spawn process, redirect StdOut

This is a discussion on Spawn process, redirect StdOut within the C# Programming forums, part of the General Programming Boards category; I'm trying to spawn cmd.exe and write its output to a file. Here's the code: Code: m_File=File.CreateText("output.txt"); proc=new Process(); proc.StartInfo.FileName="cmd.exe"; ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Spawn process, redirect StdOut

    I'm trying to spawn cmd.exe and write its output to a file. Here's the code:

    Code:
    m_File=File.CreateText("output.txt");
    	
    proc=new Process();
    proc.StartInfo.FileName="cmd.exe";
    proc.StartInfo.UseShellExecute=false;
    proc.StartInfo.RedirectStandardOutput=true;
    proc.Start();
    m_Out=proc.StandardOutput;
    
    Thread thd=new Thread(new ThreadStart(ReadStdOut));
    
    proc.WaitForExit();
    .
    .
    .
    
    public void ReadStdOut()
    {
    	for (;;)
    	{
    		string Output=m_Out.ReadLine();
    		if (null==Output)
    			break;
                   m_File.WriteLine(Output);				
    	}
    	proc.WaitForExit();
    }
    I can see an empty console flash quickly on the screen, but the process seems to end straight away, and nothing is written to the file. Shouldn't it write the prompt to the file, and then wait for something from STDIN?

    If there is an easier way to do this I would love to know.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Code:
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    
    namespace RedirectStandardOutput
    {
        class Program
        {
            static void Main(string[] args)
            {
                Process cmdProcess = new Process();
                StreamWriter outStream = new StreamWriter("output.txt");
    
                cmdProcess.StartInfo.FileName = "cmd.exe";
                cmdProcess.StartInfo.UseShellExecute = false;
                cmdProcess.StartInfo.RedirectStandardOutput = true;
    
                cmdProcess.Start();
    
                outStream.Write(cmdProcess.StandardOutput.ReadToEnd());
                
                cmdProcess.WaitForExit();
    
                outStream.Close();
            }
        }
    }
    Since stdout is being redirected, if you run this you'll see a blank console (cmd.exe), but if you feed it something like "dir", the output will end up in output.txt.

    Does that help?

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    That works nicely, thanks. I'm going to be expanding upon this basic functionality though, so I will likely have more questions.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fork, execv, spawn, or something else?
    By DavidP in forum C++ Programming
    Replies: 8
    Last Post: 01-26-2009, 03:25 PM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  5. Redirect stdout
    By GoLuM83 in forum C Programming
    Replies: 6
    Last Post: 12-15-2006, 03:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21