Use a macro to quickly update multiple references without updating each one individually.When working with a large solution that contains a number of projects, you will often have multiple project references pointing to the same assemblies. You might have an assembly that contains the exception management portion of your application. This assembly would naturally be referenced by every one of your other projects. But what if you had to move to that assembly's location? You would then need to update each and every reference to that assembly individually. This macro will automate that process for you. You will be able to run this macro and select the new location of your reference, and then the macro will look for any other references to the same assembly and update those references as well.To create this macro:Open the Macro IDE.Create a new module.Copy the following code into the module:
Imports EnvDTE Imports System.Diagnostics Imports Microsoft.VisualBasic Imports Microsoft.VisualBasic.ControlChars Imports System.Windows Imports System.Windows.Forms Imports System Public Module UpdateReferences Public Class WinWrapper Implements System.Windows.Forms.IWin32Window Overridable ReadOnly Property Handle( ) As System.IntPtr _ Implements System.Windows.Forms.IWin32Window.Handle Get Dim iptr As New System.IntPtr(DTE.MainWindow.HWnd) Return iptr End Get End Property End Class Public Sub UpdateReference( ) Dim startDir Dim newAssembLocation Dim outdirectory As String Dim stemp, Macroprojname As String Dim prjSolution As EnvDTE.Project Dim openfile As Forms.FileDialog Dim result As Forms.DialogResult Dim tlbimppath As Microsoft.Win32.RegistryKey Dim winptr As WinWrapper winptr = New WinWrapper openfile = New Forms.OpenFileDialog 'set the initial directory to SystemDrive startDir = System.Environment.SystemDirectory( ) startDir = Left(startDir, InStr(startDir, "\", _ CompareMethod.Text)) openfile.InitialDirectory = startDir If openfile.ShowDialog(winptr) = result.OK Then newAssembLocation = Right(openfile.FileName, _ Len(openfile.FileName) - _ Len(System.Environment.CurrentDirectory) - 1) End If Dim myProj As Integer Dim prjVSProject As VSLangProj.VSProject Try For myProj = 1 To DTE.Solution.Projects.Count prjVSProject = DTE.Solution.Projects.Item(myProj).Object If prjVSProject Is Nothing Then MsgBox("Unable to get reference to solution file") Exit Sub End If Dim myRef As VSLangProj.Reference ' Strip off the .dll ext so we can use the find command newAssembLocation = newAssembLocation.Replace(".dll", ") myRef = prjVSProject.References.Find(newAssembLocation) If Not myRef Is Nothing Then ' remove the old reference myRef.Remove( ) ' add the new one prjVSProject.References.Add(openfile.FileName) Else ' We did not find a reference to this assembly here End If Next Catch err As System.Exception End Try End Sub End Module
Save and close the macro.First make sure that the solution whose references you want to update is open, and then activate your newly created macro. When you run the macro, you will see the Open File dialog; using this dialog, select the new location of your assembly. Once you have selected the new location, Visual Studio will search through the solution for any other references to the same assembly. Any references found will be updated to the new location.This macro is a great time-saver when you are reorganizing projects or solutions and is a great alternative to updating all your references manually.Thanks to Doug Doedens for writing this macro and posting at the C# Corner (see http://www.c-sharpcorner.com/Code/2002/July/SolutionMacros.asp).