Sunday, 3 May 2015

Bulk "Add Reference"

My team works on a large solution and there was an interesting situation where needed to add a common reference across about 90 odd projects in the solution. There isn't a straightforward way to do that in Visual Studio yet. So, what do we do? We write a simple program, isn't it. In the end, all references go to csproj projects and csproj file is a relatively simple and human readable text file.


string directoryPath = @"D:\solutionLocation";
            DirectoryInfo dirInfo2 = new DirectoryInfo(directoryPath);
            foreach (var file in dirInfo2.GetFiles("*.csproj", SearchOption.AllDirectories))
            {
                var lines = File.ReadAllLines(file.FullName);
                for(int i = 0; i < lines.Length; i ++)
                {
                    if (lines[i].Contains("Reference Include=\"System\""))
                    {
                        lines[i] = @"False..\References\Newtonsoft.Json.dll";
                    }
                }

                File.WriteAllLines(file.FullName, lines);
            }
           

For this to work effectively against TFS, you should create a local workspace so that any file save is automatically detected as a checked-out item and starts to show up in Pending Changes window. If you use Server workspace, performance of TFS operations (get, merge etc.) may improve but you lose the feature of automatic detection of file changes.

A poor man's solution but it works.

No comments:

Post a Comment