Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding and removing multiple references across projects in Visual Studio

I have a huge project with over 50 or 60 csproj files. I need to remove 6 or 7 references exiting references and add 10 or 15 new references across all these projects. Is there a mechansim to bulk add references and remove references?

Or if there is a way to say, replace reference X with Y across all projects, that would be very helpful too. Let me add that Nuget is not an option.

like image 794
praskris Avatar asked Apr 04 '12 20:04

praskris


2 Answers

This one's easy: just write a script that opens up all your .proj files, removes the lines you don't need and add the lines you do. That's the easiest way. I think there are also options for writing plug-ins for VS or ReSharper, but both of these options are overkill. Project files are just XML files (=text files) after all, and can be treated as such.

Of course, if you find this to be a recurrent action, or if there is some conditional logic that you need to adhere to, then writing a plug-in is a sensible choice.

like image 63
Dmitri Nesteruk Avatar answered Oct 03 '22 11:10

Dmitri Nesteruk


You can add a reference to multiple projects in Visual Studio by writing a script/macro using AutoHotKey.

The method below interacts with a running copy of Visual Studio rather than manipulating the text in the project files directly, it utilises the fact you can get everything (most?) things done without a mouse and using only a keyboard. It works by sending the appropriate key presses to Visual Studio such that it finds the appropriate projects, right clicks, opens references etc.

For the script to work you need to have the Resource View window open with a project highlighted before executing the script. This is so that it can search for the projects by name (the solution explorer may have folders that prevent this). You will also need to adjust the script by renaming the project window SolutionName and the name of the reference you wish to add ReferenceName. The projects.txt is just a text file in the same directory as your AutoHotKey script with the newline separated list of projects you wish to add the reference to.

Note: I am using Visual Studio 2008. Also, I have not used any scientific method in determining my Sleep times, they may not even be necessary. This was a tactical solution and the script can no doubt be optimised and improved.

; Prepare array from project list
ArrayCount = 0 ; Initialise array counter
Loop, Read, projects.txt   ; This loop retrieves each line from the file, one at a time.
{
ArrayCount += 1  ; Keep track of how many items are in the array.
Array%ArrayCount% := A_LoopReadLine  ; Store this line in the next array element.
}
IfWinExist SolutionName - Microsoft Visual Studio (Administrator) ; Check if VS2008 with solution is running
{
WinActivate ; Bring a window to front
Loop %ArrayCount%
{
	element := Array%A_Index% ; Grabs current project name from array
	Send % element ; types the name of the project
	Sleep 100
	Send {AppsKey}F{Tab}{Tab}{Space}
	Sleep 100
	Send ReferenceName{Enter}
	Sleep 100
	Send {Enter}
	Sleep 100
}

}
else
{
MsgBox Cannot find "SolutionName - Microsoft Visual Studio (Administrator)" window.
}
like image 41
connorads Avatar answered Oct 03 '22 10:10

connorads