Ian H.
Ian H.

Reputation: 3929

C# - Write to a new Console Window

To write some sort of debug-extension for my applications I tried to make something that can create a new console window and write information to it.

I wrote a very simple ConsoleApplication, that basically reads the input it receives until it is equal to terminate.

private static void Main(string[] args)
{
    string text;
    while ((text = Console.ReadLine()) != "terminate")
    {
        Console.WriteLine(text);
    }
}

I added this ConsoleApplication to my resources and then wrote this inside a new project:

// Create the temporary file
string path = Path.Combine(Path.GetTempPath(), "debugprovider.exe");
File.WriteAllBytes(path, Properties.Resources.debugprovider);

// Execute the file
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.FileName = path;

Process process = new Process();
process.StartInfo = startInfo;
process.Start();

process.StandardInput.WriteLine("Write a new line ...");
process.StandardInput.WriteLine("terminate");
process.WaitForExit();

When my application is closing I also call this to remove the file afterwards:

// Delete the temporary file as it is no longer needed
File.Delete(path);

My problem is that I don't see the input in the window, there only exists a blank console window. How can I fix this?

Upvotes: 1

Views: 3218

Answers (2)

Pavlo
Pavlo

Reputation: 26

Try this one :

var path = Path.Combine(Path.GetTempPath(), "debugprovider.exe");
File.WriteAllBytes(path, Properties.Resources.debugprovider);
using (var cmd = new Process())
{
    cmd.StartInfo = new ProcessStartInfo(path)
    {
        WindowStyle = ProcessWindowStyle.Hidden,
        UseShellExecute = false,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        CreateNoWindow = true
    };
    var outputStringBuilder = new StringBuilder();
    cmd.OutputDataReceived += (sender, e) =>
    {
        if (!String.IsNullOrEmpty(e.Data))
        {
            Console.WriteLine(e.Data);
        }
    };
    var errorStringBuilder = new StringBuilder();
    cmd.ErrorDataReceived += (sender, e) =>
    {
        if (!String.IsNullOrEmpty(e.Data))
        {
            Console.WriteLine(e.Data);
        }
    };
    cmd.Start();
    cmd.StandardInput.WriteLine("Write a new line ...");
    cmd.StandardInput.WriteLine("terminate");
    cmd.StandardInput.Flush();
    cmd.StandardInput.Close();
    cmd.BeginOutputReadLine();
    cmd.BeginErrorReadLine();
    cmd.WaitForExit();
}

Upvotes: 0

Tim
Tim

Reputation: 6060

If you want the output of your child process, then you would want to redirect standard output to and listen to the OutputDataReceived event of process:

process.RedirectStandardOutput = true;
process.OutputDataReceived += process_OutputDataReceived;

and then handled the output from your child process in process_OutputDataReceived:

void process_OutputDataReceived(object sender, DataReceivedEventArgs e) {
    Console.Write(e.Data);
}

Upvotes: 1

Related Questions