0

A user is being asked if he / she likes to save the cmd output into a .txt-file. If this condition is true, StreamWriter starts "listening" to the cmd output until a certain point, then saves it into a .txt-file. StreamWriter should not redirect the output; it should appear in the cmd as well the specific part in the .txt-file.

Use this excerpt to get an overview over my code:

if(choice == 1)
{
    write = WriteFile.Exists(path, id); // In this method the user confirms if he likes to save the following output to the .txt-File

    if(write == true)
    {
       // START LISTENING TO OUTPUT
    }
}

// Algorithm which searches for an ID

Console.WriteLine("The ID was {0}", id);

if(write == true)
{
   // STOP LISTENING and SAVE
}

Is it possible to realize my inquriy in an easy way?

  • 1
    Depends on what is "easy" for you. Please, do some research. This problem has been solved often and I know there must be some really good answers already here on Stack Overflow. – Thomas Weller Jan 11 '18 at 11:06
  • Do you want to redirect the output of *your* process to a file or of a *different* process that your application has started? – Thomas Weller Jan 11 '18 at 11:08
  • 2
    Please explain why [this](https://stackoverflow.com/questions/11911660/redirect-console-writeline-from-windows-application-to-a-string), [this](https://stackoverflow.com/questions/38854581/redirect-console-writeline-output-to-string), [this](https://stackoverflow.com/questions/3127498/c-sharp-how-to-redirect-stream-to-the-console-out), [this](https://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net) and [that](https://stackoverflow.com/questions/18588659/redirect-process-output-c-sharp) has not solved your problem yet. – Thomas Weller Jan 11 '18 at 11:11

1 Answers1

0

I'm using a dynamic List, to which every new entry is added. At the end of the method Console.WriteLine prints out every item of the list in a for-loop. If the user wishes to save the text to a .txt-file (condition is true), it will be written into it with

using (StreamWriter print = new StreamWriter("LOG {0}.txt", id))
{
    print.WriteLine("LOG ID {0}\n\n", id);
    for (int i = 0; i < output.Capacity; i++)
    {
        print.WriteLine(output.ElementAt(i));
    }
}