Console.SetOut() Method in C#

Console.SetOut(TextWriter) Method in C# is used to redirect the stream of standard output. With the help of this method, a user can specify a StreamWriter as the output object. The Console.SetOut method will receive an object of type TextWriter. The StreamWriter can be passed to Console.SetOut and it is implicitly cast to the TextWriter type. It simply sets the standard output stream property to the specified TextWriter object it gets.

Syntax:

public static System.IO.TextWriter Out { get; }
                or
public static void SetOut (System.IO.TextWriter newOut);
                or
public static void SetOut(TextWriter newOut)

Return Value: It returns the streamWriter to specified TextWriter Object.



Exceptions:

  • When the newOut is null ArgumentNullException is thrown which does not accept it as a valid argument.
  • When an I/O error occurred IOException is thrown.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to demonstrate the use 
// of Console.SetOut method
using System;
using System.IO;
  
class GFG {
  
    // Main Method
    static void Main()
    {
  
        // Creating a text file named "out" in D Drive
        using(StreamWriter writer = new StreamWriter("D:\\out.txt"))
        {
            Console.SetOut(writer);
            Result();
        }
    }
  
    // Method Result
    static void Result()
    {
  
        // Writting to the file
        Console.WriteLine("GeeksforGeeks");
        Console.WriteLine("A Computer Science portal for Geeks!");
    }
}

chevron_right


Compiling and Executing:

Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to demonstrate the use 
// of Console.SetOut method
using System;
using System.IO;
  
class GFG {
  
    // Main Method
    static void Main()
    {
  
        // will display on console
        Console.WriteLine("\nGeeksForGeeks");
  
        // Creating a text file named "Geeks" 
        // at the location of your program
        FileStream geeks1 = new FileStream("Geeks.txt", FileMode.Create);
  
        // Standard Output stream is 
        // being saved to a Textwriter
        TextWriter geeksave = Console.Out;
  
        StreamWriter portal1 = new StreamWriter(geeks1);
        Console.SetOut(portal1);
  
        Console.WriteLine("\nThe Computer Science portal for Geeks");
  
        Console.WriteLine("\nWelcome to GeeksforGeeks");
        Console.SetOut(geeksave);
  
        // will display on console
        Console.WriteLine("This is Console.SetOut Method in C#");
        Console.WriteLine("Get programming practices at your own pace !");
        portal1.Close();
    }
}

chevron_right


Compiling and Executing:

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.