Console.SetIn() Method in C#
Last Updated :
11 Mar, 2019
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:
- ArgumentNullException: If the newIn is null.
- SecurityException: If the caller does not have the required permission.
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:
using System;
using System.IO;
class GFG {
static void Main()
{
string geeks = " " ;
using (StreamReader gfg = new StreamReader( "D:\\out.txt" ))
{
Console.SetIn(gfg);
string l;
while ((l = Console.ReadLine()) != null )
{
geeks = geeks + l;
}
Console.WriteLine( "Hello " + geeks);
Console.ReadKey();
}
}
}
|
Output:
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...