Unable to store output of executed commands inside a file c# console Application

  .net, .net-core-3.1, c++, console-application, windows

I am trying to store executed commands results inside a txt file, command execution is working fine and also showing a command prompt but I am unable to store results inside my file.

It’s only creating a empty file inside that folder.

Program.cs :

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace commandExecutionPractice
{
    class Program
    {
        static void Main(string[] args)
        {
            var folderPath = @"C:";
            var fileName = @"commandLog.txt";
            var fullPath = folderPath + fileName;
            StreamWriter writer = new StreamWriter(fullPath);

            using (CmdService cmdService = new CmdService("cmd.exe"))
            {
                string consoleCommand = String.Empty;
                do
                {
                    Console.WriteLine("Enter Command : ");
                    consoleCommand = Console.ReadLine();
                    string output = cmdService.ExecuteCommand(consoleCommand);
                    writer.WriteLine(">>> {0}", output);
                    Console.WriteLine(">>> {0}", output);
                }
                while (!String.IsNullOrEmpty(consoleCommand));
            }


            Console.ReadLine();
        }
    }
}

CmdService.cs :

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace commandExecutionPractice
{
    public class CmdService : IDisposable
    {
        private Process _cmdProcess;
        private StreamWriter _streamWriter;
        private AutoResetEvent _outputWaitHandle;
        private string _cmdOutput;

        public CmdService(string cmdPath)
        {
            _cmdProcess = new Process();
            _outputWaitHandle = new AutoResetEvent(false);
            _cmdOutput = String.Empty;

            ProcessStartInfo processStartInfo = new ProcessStartInfo();
            processStartInfo.FileName = cmdPath;
            processStartInfo.UseShellExecute = false;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.RedirectStandardInput = true;
            processStartInfo.CreateNoWindow = true;

            _cmdProcess.OutputDataReceived += _cmdProcess_OutputDataReceived;

            _cmdProcess.StartInfo = processStartInfo;
            _cmdProcess.Start();

            _streamWriter = _cmdProcess.StandardInput;
            _cmdProcess.BeginOutputReadLine();
        }

        public string ExecuteCommand(string command)
        {
            _cmdOutput = String.Empty;

            _streamWriter.WriteLine(command);
            _streamWriter.WriteLine("echo end");
            _outputWaitHandle.WaitOne();
            return _cmdOutput;
        }

        private void _cmdProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data == null || e.Data == "end")
                _outputWaitHandle.Set();
            else
                _cmdOutput += e.Data + Environment.NewLine;
        }

        public void Dispose()
        {
            _cmdProcess.Close();
            _cmdProcess.Dispose();
            _streamWriter.Close();
            _streamWriter.Dispose();
        }
    }
}

Edit : I added code of CmdService class which I forgot to add.

Thanks in Advance.

Source: Windows Questions

LEAVE A COMMENT