Where does console output go?

A

AMeador

I can see console output in the output window on VS, but how can you
see this text when running the app on a different machine or outside of
VS?

Thanks!

--- Andrew
 
G

Guest

Hi,

Can you provide more info:

1) is a console application
2) are you using the Debug object?
3) are you using the trace object?
4) are you using the Console.WriteLine function?

Cheers
Salva
 
G

Gabriel Lozano-Morán

You could use a streamwriter to redirect the output to the filesystem as
example:

using System;
using System.IO;
namespace ConsoleApplication4
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
FileStream fileStream = new FileStream(@"c:\Test.txt",
FileMode.Create);
StreamWriter streamWriter = new StreamWriter(fileStream);
Console.SetOut(streamWriter);
Console.WriteLine("Hello file");
streamWriter.Close();
}
}
}

Gabriel Lozano-Morán
Software Engineer
Sogeti
 
A

AMeador

I am doing this form a Windows Form app and using the Console.Writeline
function. Sorry, about the confusion, I didn't think about console
output from these other sources.
 
A

AMeador

Thanks - this will be useful to me. Does this mean that using
Console.Writeline function calls produce output that is only direclty
visable from within VS?
 
G

Gabriel Lozano-Morán

Behind the scenes a handle is retrieved to the active console screen buffer.
The Console uses a TextWriter to write output to the console. You can
retrieve a handle to input device to capture all the output but better is to
use a TextWriter to redirect the output.

Gabriel Lozano-Morán
Software Engineer
Sogeti
 
G

Guest

I think if you use a command line to load your GUI application and within
that application you use Console.Write(), you will see the output in the
command line window.
But if you load your application by double clicking your GUI exe, then you
won't see anything, simply because you don't have a Console open.
So its NOT ONLY within VS that you see the output.
(In java its clearer since usually you load the application from a command
line using C:\java <appName>)
-Ash
 
G

Gabriel Lozano-Morán

If you don't want to change the source code of the console application you
can start the process from within your application with the output
redirected to a stream. In this example type exit in the console when you
run it to end the process:

using System;
using System.Diagnostics;
using System.IO;

namespace ConsoleApplication7
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
StreamWriter streamWriter = new StreamWriter(@"c:\output.txt");
Process process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = @"c:\windows\system32\cmd.exe";
process.StartInfo.UseShellExecute = false;
process.Start();
streamWriter.Write(process.StandardOutput.ReadToEnd());
streamWriter.Close();
}
}
}

Gabriel Lozano-Morán
Software Engineer
Sogeti
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top