问题
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.
回答1:
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
回答2:
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" );
回答3:
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");
来源:https://stackoverflow.com/questions/7773880/how-do-i-write-to-the-output-window-in-visual-studio-2010-addin