Auto-Fix ProjectReference Include Paths Before Creating a csproj File

I have a VS 2010 solution with a number of projects. Projects refer to other projects as part of the solution. I noticed that when I have the wrong project link path in the csproj file, like this:

<ProjectReference Include="..\..\..\..\WrongFolder\OtherProject.csproj"> <Project>{CD795AA6-9DC4-4451-A8BA-29BACF847AAC}</Project> <Name>OtherProject</Name> </ProjectReference> 

Visual studio will fix this when opening the solution:

 <ProjectReference Include="..\..\..\..\RightFolder\OtherProject.csproj"> <Project>{CD795AA6-9DC4-4451-A8BA-29BACF847AAC}</Project> <Name>OtherProject</Name> </ProjectReference> 

I assume that it uses the GUID from the Project element to uniquely identify the project as part of a solution that allows it to correct the path.

MSBuild, on the other hand, does not seem to fix this path, and solution creation fails.

Is there a way to get MSBuild to fix the path, or to do it as a pre-build step using some other tool or command so that the solution builds correctly?

Thanks!

+4
source share
1 answer

This is part of the functionality of VisualStudio. But you can call the tool to resolve the links before building. Here is a draft code that you can clarify:

 using System; using System.IO; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Xml; namespace FixProjectReferences { class Program { // License: This work is licensed under a Creative Commons // Attribution-ShareAlike 3.0 Unported License. // Author: Marlos Fabris // Summary: Updates the project references in csproj. // Param: // args[0] = Main project (c:\mainProject.csproj) // args[1] = Folder to scan other projects (c:\other) static void Main(string[] args) { string mainProject = args[0]; string folder = args[1]; FileInfo mainPrjInfo = new FileInfo(mainProject); string currentDir = Directory.GetCurrentDirectory(); // Lists all project files in the directory specified // and scans the GUID's. DirectoryInfo info = new DirectoryInfo(folder); FileInfo[] projects = info.GetFiles("*.csproj", SearchOption.AllDirectories); Dictionary<Guid, string> prjGuids = new Dictionary<Guid, string>(); foreach (var project in projects) { if (project.FullName == mainPrjInfo.FullName) continue; Regex regex = new Regex("<ProjectGuid>(\\{.*?\\})</ProjectGuid>"); Match match = regex.Match(File.ReadAllText(project.FullName)); string guid = match.Groups[1].Value; prjGuids.Add(new Guid(guid), project.FullName); } // Loads the main project and verifies if the references are valid. // If not, updates with the correct ones from the list // previously generated. XmlDocument doc = new XmlDocument(); doc.Load(mainPrjInfo.FullName); XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); ns.AddNamespace("ns", "http://schemas.microsoft.com/developer/msbuild/2003"); var nodes = doc.SelectNodes("//ns:ProjectReference", ns); foreach (XmlNode node in nodes) { string referencePath = node.Attributes["Include"].Value; string path = Path.Combine(mainPrjInfo.Directory.FullName, referencePath); if (File.Exists(path)) continue; string projectGuid = node.SelectSingleNode("./ns:Project", ns).InnerText; Guid tempGuid = new Guid(projectGuid); if (prjGuids.ContainsKey(tempGuid)) { node.Attributes["Include"].Value = prjGuids[tempGuid]; } } doc.Save(mainPrjInfo.FullName); } } } 
+1
source

All Articles