Resolving incorrect project reference GUIDs

夙愿已清 提交于 2019-12-04 05:21:18
Ilya Kozhevnikov

I think a basic console app 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);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!