简体   繁体   中英

Visual Studio 2015 - EnvDTE Read ErrorList

I'm about to create a small Build Tool. The only things it should do: Try to Build the Solution and output the errors. But not I have the following problem: In case the Build fails I cannot read the ErrorList. The program gets stuck and waits until forever.

I've created a small test Class which does nothing else than creating an instance of the Visual Studio 2015, build the given solution and read out the ErrorList on Build fail.

class Class1
{
    DTE2 dte = (DTE2)System.Activator.CreateInstance(System.Type.GetTypeFromProgID("VisualStudio.DTE.14.0", true));

    public void Test()
    {
        int id = dte.LocaleID;
        //dte.MainWindow.Visible = true;

        dte.Events.BuildEvents.OnBuildDone += new _dispBuildEvents_OnBuildDoneEventHandler(BuildEvents_OnBuildDone);

        string solutionFile = @"C:\MyProjects\SolutionWithBuildErrors.sln";

        dte.Solution.Open(solutionFile);

        while (!dte.Solution.IsOpen)
            System.Threading.Thread.Sleep(100);

        Console.WriteLine("Start Build");
        dte.Solution.SolutionBuild.Build(true);
        Console.WriteLine("Finished Build");

        dte.Quit();
    }

    private void BuildEvents_OnBuildDone(vsBuildScope Scope, vsBuildAction Action)
    {
        Console.WriteLine("BuildEvents_OnBuildDone Called");

        int buildInfo = dte.Solution.SolutionBuild.LastBuildInfo;
        switch (buildInfo)
        {
            case 0:
                Console.WriteLine("Build erfolgreich");
                break;
            case 1:
                Console.WriteLine("Build fehlerhaft");

                getErrorList();
                break;
        }
    }

    private void getErrorList()
    {
        //dte.ExecuteCommand("View.ErrorList", " ");

        Console.WriteLine("Lade Tool Windows");
        ToolWindows tw = dte.ToolWindows;
        Console.WriteLine("Geladen, Tool Windows");

        Console.WriteLine("Lade ErrorList");
        ErrorList el = tw.ErrorList;
        Console.WriteLine("Geladen, ErrorList");

        el.ShowErrors = true;

        Console.WriteLine("Lese Error Liste");

        //dte.ExecuteCommand("View.ErrorList", " ");
        //ErrorItems errors = dte.ToolWindows.ErrorList.ErrorItems;
        Console.WriteLine("#Errors: " + dte.ToolWindows.ErrorList.ErrorItems.Count);
        for (int i = 1; i <= dte.ToolWindows.ErrorList.ErrorItems.Count; i++)
        {
            ErrorItem ei = dte.ToolWindows.ErrorList.ErrorItems.Item(i);
            string errorLevel = "N/A";
            errorLevel = ei.ErrorLevel.ToString();

            string desc = "N/A";
            if (ei.Description != null)
                desc = ei.Description.ToString();

            string file = "N/A";
            if (ei.FileName != null)
                file = ei.FileName.ToString();

            string line = "N/A";
            line = ei.Line.ToString();

            string error = string.Format("{0}: {1}, File: {2}, Line: {3}", errorLevel, desc, file, line);

            Console.WriteLine(error);
        }
    }
}

For testing purposes, just create a console application. In the main:

Class1 c1 = new Class1();
c1.Test();

Console.ReadLine();

Necessary Imports: EnvDTE EnvDTE80

I've already tried to run the Visual Studio in Visible-Mode and in case the Visual Studio Instance gets the focus while in the "wait for ErrorList Read" the ErrorList can be read. If the Visual Studio never gets the focus (because running invisible or never click into while running visible) its not possible to receive the ErrorList.

Maybe there is another way to read out the ErrorList? Just found the Solution I'm using =(

Maybe you can help me out or verify that there are really troubles with the ErrorList.

This is another way to get at the ErrorList - if that's really your problem:

        EnvDTE.Window window = this.dte.Windows.Item(EnvDTE80.WindowKinds.vsWindowKindErrorList);
        EnvDTE80.ErrorList sel = (EnvDTE80.ErrorList)window.Selection;

But both ways should be fairly equivalent. Microsoft did re-write Error List window implementation for VS 2015 - introducing some issues in the process, so I'd suggest trying your code against earlier versions.

Whether this issue has been Resolved or not I don't Know, But If its Exist Then Proceed with the ActiveX Concept To Solve this issue(More or less you can proceed with User Control ). Sure this issue will resolve I have done the same for my requirement.No need to Keep Focus on the respective Visual Studio

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM