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

試著忘記壹切 提交于 2019-11-27 14:37:44

问题


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.


回答1:


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.




回答2:


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();
}



回答3:


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);
        }
    }
}



回答4:


What about Console.Clear()?



来源:https://stackoverflow.com/questions/2391473/can-the-visual-studio-debug-output-window-be-programmatically-cleared

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!