Reputation: 107
I am trying to write in the file using console. Successfully i completed it. But the problem is, I cannot write in the console after closing the connection between my file. I properly disconnected with the file. Is there any problem in my code???
var outfilename = Path.Combine(Environment.CurrentDirectory, "name.txt");
var outname = new StreamWriter(outfilename);
Console.SetOut(outname);
Console.WriteLine("aiiii");
outname.Close();
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()));
Console.Write("hai");
string nam1=Console.ReadLine();
Console.WriteLine(nam1);
Console.ReadLine();
Upvotes: 1
Views: 286
Reputation:
Use:
StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Upvotes: 1
Reputation: 30022
You can save the default Output writer and reset it:
var defaultOut = Console.Out;
var outfilename = Path.Combine(Environment.CurrentDirectory, "name.txt");
var outname = new StreamWriter(outfilename);
Console.SetOut(outname);
Console.WriteLine("aiiii");
outname.Close();
Console.SetOut(defaultOut);
Console.Write("hai");
string nam1 = Console.ReadLine();
Console.WriteLine(nam1);
Console.ReadLine();
Upvotes: 0