Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 18th, 2007, 01:04 PM
Registered User
 
Join Date: Feb 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default accessing windows application from console

i want to access bat file from the windows application..
i shall be passing multiple arguments from the environment...
basically i shall be running a code review tool and then i want to store the output from the console in an intermediate file for further processing...
can u suggest me ways to do it ?
i tried using the system "process" class and then creating a process and running it from environment... i had given the command ">a.txt" in the arguments list to create the text file of the output.. but a blank file of the name a.txt was created. and on console the same command runs without error and proper text file is created. can u help me out ? or suggest other methods to save the output from console to process it ( mayb as a string or sumthin )
thanks ppl
 
Old February 21st, 2007, 07:12 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi Anand,

There is a way to redirect the output of a console window you start from an application, by using the ProcessStartInfo class with your Process object. You can find the full details on MSDN:
http://msdn2.microsoft.com/en-us/lib...ardoutput.aspx

A simple example:

Code:
// create the process
Process myproc = new Process();
// set file and arguments
myproc.StartInfo.FileName = "myconsole.exe";
myproc.StartInfo.Arguments = "someargument";
// this must be false to let us get output
myproc.StartInfo.UseShellExecute = false;
// tell it to redirect
myproc.StartInfo.RedirectStandardOutput = true;

// start process up
myproc.Start();    
// get the output
string output = myprocess.StandardOutput.ReadToEnd();
// let process finish
myproc.WaitForExit();

/* output is now a string, so you can save to a file */
If you're not careful, you can get confusing problems with the 2 programs deadlocking each other, e.g. both waiting for input, but the MSDN page is fairly good at explaining it.

Hope this helps
Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
Console application but no Console gamotter C# 2 August 22nd, 2007 11:41 PM
Console windows tarzannn C# 4 May 24th, 2007 01:43 AM
Windows Service and Console Application ndramkumar General .NET 0 March 26th, 2006 11:26 PM
Console Application digby_dog VB.NET 2002/2003 Basics 2 April 29th, 2005 10:52 AM
Console application and web application nsrujan ASP.NET 1.0 and 1.1 Basics 1 April 16th, 2005 10:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.