Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Save Console.WriteLine Output to Text File

Tags:

c#

I have a program which outputs various results onto a command line console.

How do I save the output to a text file using a StreamReader or other techniques?

System.Collections.Generic.IEnumerable<String> lines = File.ReadAllLines(@"C:\Test\ntfs8.txt");  foreach (String r in lines.Skip(1)) {     String[] token = r.Split(',');     String[] datetime = token[0].Split(' ');     String timeText = datetime[4];     String actions = token[2];     Console.WriteLine("The time for this array is: " + timeText);     Console.WriteLine(token[7]);     Console.WriteLine(actions);     MacActions(actions);     x = 1;     Console.WriteLine("================================================"); }  if (x == 2) {     Console.WriteLine("The selected time does not exist within the log files!"); }  System.IO.StreamReader reader = ; string sRes = reader.ReadToEnd(); StreamWriter SW; SW = File.CreateText("C:\\temp\\test.bodyfile"); SW.WriteLine(sRes); SW.Close(); Console.WriteLine("File Created"); reader.Close(); 
like image 961
JavaNoob Avatar asked Dec 17 '10 13:12

JavaNoob


People also ask

How do I save a console Output to a text file?

the shortcut is Ctrl + Shift + S ; it allows the output to be saved as a text file, or as HTML including colors!

How do I save the Output console in R?

Saving your workspace is how you save your data within R. Click on the Console window, go to the File menu and select “Save Workspace...”. In another R session, you open this workspace with the “Load Workspace...” command. To save everything that has scrolled past on the Console window, click on the Console window.


1 Answers

Try this example from this article - Demonstrates redirecting the Console output to a file

using System; using System.IO;  static public void Main () {     FileStream ostrm;     StreamWriter writer;     TextWriter oldOut = Console.Out;     try     {         ostrm = new FileStream ("./Redirect.txt", FileMode.OpenOrCreate, FileAccess.Write);         writer = new StreamWriter (ostrm);     }     catch (Exception e)     {         Console.WriteLine ("Cannot open Redirect.txt for writing");         Console.WriteLine (e.Message);         return;     }     Console.SetOut (writer);     Console.WriteLine ("This is a line of text");     Console.WriteLine ("Everything written to Console.Write() or");     Console.WriteLine ("Console.WriteLine() will be written to a file");     Console.SetOut (oldOut);     writer.Close();     ostrm.Close();     Console.WriteLine ("Done"); } 
like image 196
Adriaan Stander Avatar answered Oct 19 '22 17:10

Adriaan Stander