How to display console output in output window in Visual Studio Addin?

2k views Asked by At

I am developing a Visual Studio plugin. It will automatically generate and run a command line. If I run the command in the shell, it could generate some logs during running.

However, I want to hide the shell window and display the logs in the Visual Studio Output Window. Is there a way to implement this?

Here's my code to run the command:

var process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c"+command; 
process.Start();
2

There are 2 answers

0
bowlturner On

according to this similar question

Change application type to Windows before debugging. Without Console window, Console.WriteLine works like Trace.WriteLine. Don't forget to reset application back to Console type after debugging.

4
AzNjoE On

This might help:

process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.startInfo.RedirectStandardOutput = true;
process.startInfo.RedirectStandardError = true;

StreamReader stringBackFromProcess = process.StandardOutput;

Debug.Write(stringBackFromProcess.ReadToEnd());

// or

Console.Write(stringBackFromProcess.ReadToEnd());