A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/console-setin-method-in-c-sharp/ below:

Console.SetIn() Method in C# - GeeksforGeeks

Console.SetIn() Method in C#

Last Updated : 11 Jul, 2025

The

Console.SetIn() method

is used to set the

In

property of the specified StreamReader object i.e. it redirects the standard input from the console to the input file. Since the console is set with this StreamReader object, the

ReadLine() method

can be called to read the contents of the file, line by line.

Syntax: public static void SetIn (System.IO.StreamReader newIn); Parameter: newIn: It is a stream which is the new standard input.
Exceptions: Example:

In this example, the SetIn() method is used to set the StreamReader object to the console, and the file's content will be read, stored and printed.

Text File Used: csharp
// C# program to demonstrate SetIn() method
using System;
using System.IO;

class GFG {

    // Main Method
    static void Main()
    {

        // Create a string to get 
        // data from the file
        string geeks = " ";

        // creating the StreamReader object
        // and set it to the desired 
        // text file
        using(StreamReader gfg = new StreamReader("D:\\out.txt"))
        {

            // setting the StreamReader 
            // object to the Console
            Console.SetIn(gfg);

            string l;

            // Reading the contents of the file into l
            while ((l = Console.ReadLine()) != null) 
            {
                geeks = geeks + l;
            }

            // Printing the file contents
            // appended to "Hello "
            Console.WriteLine("Hello " + geeks);

            // Waiting for user input
            // to exit the program
            Console.ReadKey();
        }
    }
}
Output: Reference:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4