locked
How do I show and save my console output in textfile as well as show in command prompt RRS feed

  • Question

  • User-1971168174 posted

    Hi,

    Can someone help me with example , I need to show output for my console application and at the end need to save entire command prompt result in a text file.

    I tried using below code but this does not show result in command prompt , only writes to text file.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace test
    {
        class Program
        {
            static void Main(string[] args)
            {
                string UserName = System.Environment.UserName;
                string fileName = @"C:\Users\nm\Documents\nm.txt";
                FileStream ostrm;
                StreamWriter writer;
                TextWriter oldOut = Console.Out;
                try
                {
                    ostrm = new FileStream(fileName, 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");
            }
        }
    }
    

    Thanks!

    Thursday, June 18, 2020 8:09 AM

Answers

  • User-719153870 posted

    Hi neerajkumarmodi,

    this does not show result in command prompt , only writes to text file.

    This is expected behaviour, please check Console Class and you will find "To redirect the standard input, standard output, or standard error stream, call the Console.SetIn, Console.SetOut, or Console.SetError method, respectively.". This means the Console.SetOut will redirect the standard stream and that's why the Console.WriteLine doesn't write naything in command prompt.

    Thus, to show these in console output, i would suggest to read the txt file and Console.WriteLine again. Like below:

            static void Main(string[] args)
            {
                string UserName = System.Environment.UserName;
                string fileName = @"D:\nm.txt";
                FileStream ostrm;
                StreamWriter writer;
                TextWriter oldOut = Console.Out;
                try
                {
                    ostrm = new FileStream(fileName, 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();
                var files = File.ReadAllLines(fileName);
                files.ToList().ForEach(s => Console.WriteLine(s));
                Console.WriteLine("Done");
            }

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 18, 2020 8:55 AM

All replies

  • User-719153870 posted

    Hi neerajkumarmodi,

    this does not show result in command prompt , only writes to text file.

    This is expected behaviour, please check Console Class and you will find "To redirect the standard input, standard output, or standard error stream, call the Console.SetIn, Console.SetOut, or Console.SetError method, respectively.". This means the Console.SetOut will redirect the standard stream and that's why the Console.WriteLine doesn't write naything in command prompt.

    Thus, to show these in console output, i would suggest to read the txt file and Console.WriteLine again. Like below:

            static void Main(string[] args)
            {
                string UserName = System.Environment.UserName;
                string fileName = @"D:\nm.txt";
                FileStream ostrm;
                StreamWriter writer;
                TextWriter oldOut = Console.Out;
                try
                {
                    ostrm = new FileStream(fileName, 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();
                var files = File.ReadAllLines(fileName);
                files.ToList().ForEach(s => Console.WriteLine(s));
                Console.WriteLine("Done");
            }

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 18, 2020 8:55 AM
  • User-1971168174 posted

    Thanks Yang,

    I need to read all lines from command prompt and write in the file.

    Thursday, June 18, 2020 11:28 AM