Resolving Incorrect Project GUIDs

I have some projects in a Visual Studio solution that ended up with references to projects that include the wrong GUID for the specified project. (Perhaps due to the recreated link project at some point)

eg. Consider a CoreProject.csproj project with the following properties:

<ProjectGuid>{93803F9C-8C65-4949-8D44-AB7A3D0452C8}</ProjectGuid> <OutputType>Library</OutputType> <RootNamespace>CoreProject</RootNamespace> <AssemblyName>CoreProject</AssemblyName> 

Another project includes a link to this, but at some point the GUID has changed and is now incorrect.

 <ProjectReference Include="..\CoreProject\CoreProject.csproj"> <Project>{5FD52517-79F8-41D2-B6F2-EA2D8A886549}</Project> <Name>CoreProject</Name> </ProjectReference> 

The solution is still loading and building correctly in Visual Studio and msbuild, but I suspect that incorrect GUIDs may have some performance impact in VS.

The solution is quite large with many projects that have this problem, and I would prefer not to add these links manually. Are there any tools or macros that can β€œfix” the project’s GUID links?

+6
source share
1 answer

I think the base console application should do the trick, something like this:

 using System; using System.IO; using System.Linq; using Microsoft.Build.Evaluation; public class Program { public static void Main(String[] args) { var projects = Directory.EnumerateFiles(@"...", "*.csproj", SearchOption.AllDirectories) .Select(x => { try { return new Project(x); } catch { return null; } }) .Where(x => x != null) .ToList(); var guids = projects.ToDictionary(p => p.FullPath, p => p.GetPropertyValue("ProjectGuid")); foreach (var project in projects) { var save = false; foreach (var reference in project.GetItems("ProjectReference")) { var path = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullPath), reference.EvaluatedInclude)); if (!guids.ContainsKey(path)) { Console.WriteLine("No {0}", Path.GetFileName(path)); continue; } var guid = guids[path]; var meta = reference.GetMetadataValue("Project"); if (meta != guid) { Console.WriteLine("{0} -> {1}", meta, guid); reference.SetMetadataValue("Project", guid); save = true; } } if (save) project.Save(project.FullPath); } } } 
+7
source

Source: https://habr.com/ru/post/917014/


All Articles