11

I'm writing a simple Visual Studio 2010 Add-In to do a common copying job here at work (getting dlls from libs sln).

I want the progress of the copying to be written to the output window.

I tried Trace.WriteLine(...) expecting that to make it, but it doesn't when I run the add-in in the debugger. I have not tried it any other way yet.

I found some examples of doing that in Visual Studio 2008, but the required libs are not available to reference.

Can anyone point me to how to write to the output window? My googling skills have failed me.

Eric Brown - Cal
  • 14,135
  • 12
  • 58
  • 97

3 Answers3

17

I've done this for a macro I wrote:

Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
OutputWindow outputWindow = (OutputWindow) window.Object;
outputWindow.ActivePane.Activate();
outputWindow.ActivePane.OutputString(message);

Here is a link for the DTE Interface: http://msdn.microsoft.com/en-us/library/envdte.dte(v=VS.100).aspx

John Cornell
  • 1,087
  • 8
  • 22
  • What is the object dte? (reading docs now, but it'd be nice in the answer for future reference) – Eric Brown - Cal Oct 17 '11 at 14:56
  • 1
    For those who follow, dte, is defined in the connect.cs, on connect method. Look for this towards the bottom of the file: private DTE2 _applicationObject; Use that as dte – Eric Brown - Cal Oct 17 '11 at 15:03
8

As Robert pointed out, John's code will throw an exception when there is no ActivePane. If there is an active pane, it will use whichever pane is active.

One issue I have with Robert's example is depending on where you create the pane, which in my case is the Exec method, it will create multiple panes with the same name each time it is run.

Including my example as to how I got around that issue. Pretty simple, just check for the existence of the window first...

      Window           window           = _applicationObject.Windows.Item( EnvDTE.Constants.vsWindowKindOutput );
      OutputWindow     outputWindow     = ( OutputWindow )window.Object;
      OutputWindowPane outputWindowPane = null;

      for ( uint i = 1; i <= outputWindow.OutputWindowPanes.Count; i++ )
      {
        if ( outputWindow.OutputWindowPanes.Item( i ).Name.Equals( OUTPUT_WINDOW_NAME , StringComparison.CurrentCultureIgnoreCase ) )
        {
          outputWindowPane = outputWindow.OutputWindowPanes.Item( i );
          break;
        }
      }

      if ( outputWindowPane == null )
        outputWindowPane = outputWindow.OutputWindowPanes.Add( OUTPUT_WINDOW_NAME );

      outputWindowPane.OutputString( "Message" );
David Cardinale
  • 265
  • 2
  • 6
1

I am writing a Visual studio add-in and had the same problem, however when trying the above answer I found that the line:

outputWindow.ActivePane.Activate();

gave an error.

NullReferenceException -- Object reference not set to an instance of an object.

However I have now found a slightly different way to solve the problem:

Window window = applicationObject.Windows.Item(Constants.vsWindowKindOutput);
OutputWindow outputWindow = (OutputWindow)window.Object;
OutputWindowPane owp;
owp = outputWindow.OutputWindowPanes.Add("new pane");
owp.OutputString("hello");
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
robert evans
  • 43
  • 1
  • 5