我即將創建一個小型構建工具。它應該做的唯一事情:嘗試構建解決方案並輸出錯誤。 但不是我有以下問題:如果生成失敗,我無法讀取ErrorList。程序卡住了,一直等到永遠。Visual Studio 2015 - EnvDTE閱讀錯誤列表
我創建了一個小的測試類,除了創建Visual Studio 2015的實例外,沒有其他任何操作,構建給定的解決方案並讀取構建失敗時的ErrorList。
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);
}
}
}
出於測試目的,只需創建一個控制檯應用程序。在主:
Class1 c1 = new Class1();
c1.Test();
Console.ReadLine();
必要進口: EnvDTE EnvDTE80
我已試過在可見模式和運行的情況下在Visual Studio中的Visual Studio實例獲得焦點,而在「等待ErrorList Read「可以讀取ErrorList。 如果Visual Studio永遠不會獲得焦點(因爲運行不可見或從不點擊進入運行時可見),它不可能接收ErrorList。
也許有另一種方式來讀出ErrorList? 剛剛發現我使用=(
也許你能幫助我,或驗證有真與ErrorList煩惱的解決方案