Although the Visual Studio IDE is a convenient way to develop applications, sometimes a better way to do builds is on the command line. This can often be easier and faster. However, if there are errors (or warnings), it would be nice to be able to fix these using the IDE. There didn't appear to be any way to load the build log files into Visual Studio, so I decided to write a macro to do this. I decided that this should either be invoked from the Tools menu, or by right clicking in the build window. This would ask for a file, which would be loaded into the build window. It would then be possible to view the source code associated with the error, either by double clicking it or by pressing F8, in exactly the same way as if the solution/project had been built within the IDE.
The code consists of a method which asks the user for a file to load, and reads the file line by line. Each line is then written to the output window as it is read. I decided to write a class for the output window. The constructor to this class takes a name, which in this case will be "build", but we could use names like "debug" or even specify a new name to create a new pane in the output window. The only service I have written in this class is
Writeline which writes a line of text and then follows it with CR/LF. Other actions are achieved by accessing the
Pane property, and calling services on this object.
Public Class NamedOutputPane
Private win As OutputWindowPane
Public ReadOnly Property Pane() As OutputWindowPane
Sub New(ByRef WindowName As String)
win = Nothing
Dim DTE As EnvDTE.DTE
DTE = System.Runtime.InteropServices.Marshal. _
Dim mwin As Window = DTE.Windows.Item(EnvDTE.Constants _
Dim OW As OutputWindow = mwin.Object
Dim tmpOWp As OutputWindowPane
For Each tmpOWp In OW.OutputWindowPanes
If tmpOWp.Name = WindowName Then
win = tmpOWp
If win Is Nothing Then
win = OW.OutputWindowPanes.Add(WindowName)
Sub OutputTextLine(ByRef txt As String)
The other method that I needed, that I wrote as a stand alone function, was to bring up a file open dialog, and ask for the name of the log file to read. This returns the name selected or
Nothing if cancel was pressed. This uses the class
WinWrapper in order to find the parent window. If you don't do this, the dialog won't always get the focus, and may be hidden behind the main window. I found this code in the sample macros.
Function AskForFileName() As String
Dim winptr As WinWrapper = New WinWrapper
Dim openfile As Forms.FileDialog = New Forms.OpenFileDialog
If openfile.ShowDialog(winptr) = Forms.DialogResult.OK Then
AskForFileName = openfile.FileName
Catch err As System.Exception
And then finally the main method. Having done all the groundwork, all that needs to be done is to call the
getname function and read the file line by line. Each line is written to the output window as it is read.
Dim FileName As String = AskForFileName()
If Not FileName Is Nothing Then
Dim OutWin As NamedOutputPane = New NamedOutputPane("Build")
Dim sr As StreamReader = New StreamReader(FileName)
Dim line As String
line = sr.ReadLine()
Loop Until line Is Nothing
Catch E As Exception
OutWin.Pane().OutputString("The file could not be read:")
And that's it! All that remained was to add it to the Tools menu and the context (right mouse) menu. I did this by making it an add-in. The Tools menu option I did by selecting the right option in the wizard, and the right mouse menu is done using the following code:
CommandObj = applicationObject.Commands.AddNamedCommand(objAddIn, _
"Read Log File", _
"Read log from file into build window", _
False, 101, Nothing, 1 + 2)
Item("Output Window Pane"))
The errors and warnings behave just as if the code had been built within the IDE, so the code can be viewed by double clicking on the error, pressing F8 etc.
- 01 Jan 2004 - Initial version.
Jon is a Software engineer with over 25 years of experience, the last 9 of which have been using C# and ASP.NET. Previously he has used C++ and MFC. He has a degree in Electronic Systems Engineering.