Can the Visual Studio (debug) Output window be programmatically cleared?
Asked Answered
H

5

21

is it possible to have a way to clear the Visual Studio OUTPUT window, programmatically? For example, the SysInternal's debugger app called DebugView has the specific command called DBGVIEWCLEAR .. which clears the log window.

Please don't say: right-click, clear window .. with the mouse. I know that, but that's not what i'm after.

Hatchway answered 6/3, 2010 at 5:39 Comment(1)
I have looked in the past and came up short. hopefully someone is more persistent and can shed some light...Jellaba
Y
8

For VS 2008 try this code

EnvDTE80.DTE2 ide = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.9.0");
ide.ExecuteCommand("Edit.ClearOutputWindow", "");
System.Runtime.InteropServices.Marshal.ReleaseComObject(ide);

"VisualStudio.DTE.9.0" will change from VS version to version.

Yoon answered 6/3, 2010 at 5:57 Comment(5)
what assembly / namespace to i need to reference for EnvDTE80? (btw. i'm on VS2010).Hatchway
Added EnvDTE.dll and EnvDTE80.dll.Yoon
So this means this code is VERY Visual Studio version dependent? (And it doesn't work in VS2010 RC ... even when i change to GetActiveObject("VisualStudio.DTE.10.0"); Compiles and runs, but doesn't clear that window.Hatchway
I Lie. it does clear the window. No idea why it didn't the first time around.Hatchway
MIGHT want to explain where the heck this code goes. Just sayingChristabella
C
3

For VS 2010 :

//Add reference EnvDTE100
static void ClearOutput()
{
    EnvDTE80.DTE2 ide = (EnvDTE80.DTE2)Marshal.GetActiveObject("VisualStudio.DTE.10.0");
    ide.ToolWindows.OutputWindow.ActivePane.Clear();
}
Concerted answered 29/3, 2012 at 11:6 Comment(0)
T
2

The first answer works on any release after Visual Studio 2005, but it seems a little flaky. I had to put a 1 second delay before clearing the console and couldn't get it any better than that. No idea why, but it's better than nothing. It also only works if you're only running one instance of Visual Studio. Maybe I"ll make an extension that looks at the RunningObjectTable to pick the right version.

At any rate, this works more or less.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;


namespace VisualStudioHelper {
    public class VstHelper {
        // Add a Project Reference to "Microsoft Development Environment Properties 8.0" 
        // (the one for Visual Studio, not SQL Server)
        public static void VstClearOutputWindow() {
            if (!Debugger.IsAttached)
                return;

            Application.DoEvents();
            Thread.Sleep(1000);
            EnvDTE80.DTE2 ide = (EnvDTE80.DTE2)Marshal.GetActiveObject("VisualStudio.DTE.10.0");
            ide.ExecuteCommand("Edit.ClearOutputWindow", "");
            Marshal.ReleaseComObject(ide);
        }
    }
}
Timmy answered 4/5, 2011 at 16:23 Comment(3)
i have "Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))" in EnvDTE80.DTE2 ide = (EnvDTE80.DTE2)Marshal.GetActiveObject("VisualStudio.DTE.10.0");Donatello
That string will only work with Visual Studio 2010 or later. If you're using an earlier version, you'd have to change the .10. to another number. If you are running 2010 or later, it should work. You can use my completely undocumented wademan.com/Tools/RunningObjectTableDump.zip utility to see which objects are actually currently running. I haven't used the code in this post in a long time, so I've never tested it with 2013.Timmy
i think my problem is that i tried to do it with sharepoint saver on aspx with cskdevDonatello
V
0

For VS 2019 try this code

//Add reference to Interop.EnvDTE100

EnvDTE80.DTE2 ide = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE");

ide.ExecuteCommand("Edit.ClearOutputWindow", ""); System.Runtime.InteropServices.Marshal.ReleaseComObject(ide);

Vedetta answered 21/2, 2020 at 19:28 Comment(0)
L
-2

What about Console.Clear()?

Lanni answered 6/3, 2010 at 5:56 Comment(1)
That only works in a console app :( #766673Hatchway

© 2022 - 2024 — McMap. All rights reserved.