Introduction
This article show you how to write a macro that refreshes all the references of a solution. This is useful when you have a lot of projects with interrelated DLL references between them. So when you have DLL versions problems, you refresh all the references of the solution an solve it.
Using the code
Take the code showed below and paste it in the Macro IDE (Alt + F11) to create a macro.
RefreshProjectReferences
Sub
This function runs the macro code. Obtains the solution projects, display a processing message and iterates between projects refreshing the references. Uses the DTE Status Bar to show a progress bar and a progress message.
Sub RefreshProjectReferences()
Dim oVSProject As VSProject
Dim frmMessage As PopupMessage
Try
frmMessage = New PopupMessage("Refreshing references")
frmMessage.Show()
Catch ex As Exception
End Try
For i = 1 To DTE.Solution.Projects.Count
DTE.StatusBar.Progress(True, "Refreshing projects references", _
i, DTE.Solution.Projects.Count)
oVSProject = CType(DTE.Solution.Projects.Item(i).Object, VSProject)
If Not oVSProject Is Nothing Then
oVSProject.Refresh()
End If
Next
DTE.StatusBar.Progress(False)
DTE.StatusBar.Text = "Refreshing succeed"
DTE.StatusBar.Highlight(True)
Try
frmMessage.Close()
frmMessage.Dispose()
frmMessage = Nothing
Catch ex As Exception
End Try
End Sub
PopupMessage
Class
This class defines a Popup Window that show a message during refresh execution.
Note: The look & feel of this window is very simple, you can work on it for a best view.
Public Class PopupMessage
Inherits System.Windows.Forms.Form
Private txtMessage As New System.Windows.Forms.TextBox
Public Sub New(ByVal pMessage As String)
Try
Me.TopMost = True
Me.ControlBox = False
Me.FormBorderStyle = _
System.Windows.Forms.FormBorderStyle.Fixed3D
Me.Text = ""
Me.ShowInTaskbar = False
txtMessage.Top = 20
txtMessage.Left = 40
txtMessage.Width = 160
txtMessage.Font = New System.Drawing.Font("Arial", 10, _
System.Drawing.FontStyle.Bold)
txtMessage.BackColor = System.Drawing.SystemColors.Control
txtMessage.ForeColor = System.Drawing.Color.Black
txtMessage.BorderStyle = _
System.Windows.Forms.BorderStyle.None
txtMessage.Text = pMessage
txtMessage.AutoSize = True
txtMessage.SelectionLength = 0
Me.Controls.Add(txtMessage)
Me.Height = txtMessage.Height + 40
Me.Width = txtMessage.Width + 80
Me.StartPosition = _
System.Windows.Forms.FormStartPosition.CenterScreen
txtMessage.Refresh()
Me.Refresh()
Catch ex As System.Exception
End Try
End Sub
End Class
Tip
After you paste the code in the Macro Editor is useful assign a key combination to the macro (if you plan to use very much times the macro).
To do this:
- Open the menu Tools/Options
- Go to the folder Enviroment and select the Keyboard item
- Then select the macro name (e.g. Macros.Samples.VSEditor.RefreshProjectReferences)
- Select the shortcut key combination and assign it
When you select the shortcut key combination, VS will show you if it's already used and in the "Use new shortcut in" you can select the valid shortcut context.
Points of Interest
The only thing I want to remark is that macros makes extensible the Visual Studio environment and if you take a minutes to write a little code you can automate several repetitive actions.