How to reset redirection ...

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

Hello,

I have redirected standard output to disk file using the below code snippet

SteramWriter fileout;
try
{
fileout=new StreamWriter("logfile.txt");
}
catch(IOException ioe)
{
Console.WriteLine(ioe.Message + " cannot open file");
return;
}
Console.SetOut(fileout);
Console.WriteLine("Beginning of the file");
for(int i=0;i<10;i++)
Console.WriteLine(i);
Console.WriteLine("End of the file");
fileout.Close();

How to reset the standard output redirected to filestream to standard output
device back?

Thank you

Regards

Raj
 
Hello,

I have redirected standard output to disk file using the below code snippet

SteramWriter fileout;
try
{
    fileout=new StreamWriter("logfile.txt");}

catch(IOException ioe)
{
    Console.WriteLine(ioe.Message + " cannot open file");
    return;}

    Console.SetOut(fileout);
    Console.WriteLine("Beginning of the file");
    for(int i=0;i<10;i++)
        Console.WriteLine(i);
    Console.WriteLine("End of the file");
    fileout.Close();

How to reset the standard output redirected to filestream to standard output
device back?

you might just (R)ead (T)he code in the help (F)ile under
console.setout. (M)an!
 
news.microsoft.com said:
Hello,

I have redirected standard output to disk file using the below code
snippet

SteramWriter fileout;
try
{
fileout=new StreamWriter("logfile.txt");
}
catch(IOException ioe)
{
Console.WriteLine(ioe.Message + " cannot open file");
return;
}
Console.SetOut(fileout);
Console.WriteLine("Beginning of the file");
for(int i=0;i<10;i++)
Console.WriteLine(i);
Console.WriteLine("End of the file");
fileout.Close();

How to reset the standard output redirected to filestream to standard
output device back?

Thank you

Regards

Raj

Hi,

You can either save the value of Console.Out before changing it (so You can
restore it later), or You can use Console.OpenStandardOutput to open the
standard output stream again.

Hope You find this useful.
-Zsolt
 
Thank you for your suggestion dear!
Hello,

I have redirected standard output to disk file using the below code
snippet

SteramWriter fileout;
try
{
fileout=new StreamWriter("logfile.txt");}

catch(IOException ioe)
{
Console.WriteLine(ioe.Message + " cannot open file");
return;}

Console.SetOut(fileout);
Console.WriteLine("Beginning of the file");
for(int i=0;i<10;i++)
Console.WriteLine(i);
Console.WriteLine("End of the file");
fileout.Close();

How to reset the standard output redirected to filestream to standard
output
device back?

you might just (R)ead (T)he code in the help (F)ile under
console.setout. (M)an!
 
This how I did:
using System;

using System.IO;

class test

{

public static void Main()

{

StreamWriter fileout;

try

{

fileout = new StreamWriter("logfile.txt");

}

catch (Exception e)

{

Console.WriteLine(e.Message + " cannot proceed!");

return;

}

Console.WriteLine("Starting...");

Console.SetOut(fileout);

Console.WriteLine("Beginning of the file");

Console.WriteLine(1);

Console.WriteLine(2);

Console.WriteLine(3);

Console.WriteLine("End of the file");

fileout.Close();

fileout=new StreamWriter(Console.OpenStandardOutput());

fileout.AutoFlush=true;

Console.SetOut(fileout);

Console.WriteLine("End...");

}

}

Thank you

Regards

Raj
 
Thank you for your suggestion dear!

Not to worry, everyone suffers 'documentation blindness' from time to
time.
But get used to double checking the docs to see if you missed
something,
not all employers allow internet usage.

I think it's better practice to keep and restore the original state
rather than
forcing a switch to standard output. It's ok for small programs where
you know
what's going on, but as programs get larger it gets harder to predict
what state
the system is in when the call is made, it's better to play safe.

FWIW, I'd probably put the resetting of the console in a finally block
as well.
 
Pretty nice suggestion dear!

Thanks indeed

Regards

Raj

Thank you for your suggestion dear!

Not to worry, everyone suffers 'documentation blindness' from time to
time.
But get used to double checking the docs to see if you missed
something,
not all employers allow internet usage.

I think it's better practice to keep and restore the original state
rather than
forcing a switch to standard output. It's ok for small programs where
you know
what's going on, but as programs get larger it gets harder to predict
what state
the system is in when the call is made, it's better to play safe.

FWIW, I'd probably put the resetting of the console in a finally block
as well.
 
Back
Top