Parse ProjectDependency from .sln file

2019-08-07 15:18发布

问题:

Project("{00000000-0000-0000-0000-000000000000}") = "Test Projects", "TestProjects", "{5FAF487E-B913-4D46-983E-CB13FDF36FCB}"
EndProject
Project("{11111111-1111-1111-1111-111111111111}") = "ProjPropHelperAPI", "ProjPropHelperAPI\ProjPropHelperAPI.vcxproj", "{22222222-2222-2222-2222-222222222222}"
EndProject
Project("{33333333-3333-3333-3333-333333333333}") = "ProjectPropertiesHelperAPI", "ProjectPropertiesHelperAPI\ProjectPropertiesHelperAPI.csproj", "{66666666-6666-6666-6666-666666666666}"
    ProjectSection(ProjectDependencies) = postProject
        {44444444-4444-4444-4444-444444444444} = {44444444-4444-4444-4444-444444444444}
        {55555555-5555-5555-5555-555555555555} = {55555555-5555-5555-5555-555555555555}
    EndProjectSection
EndProject

I can read the Project Names, their GUIDs and Paths using the solution mentioned to this question. The name, guid and path is accessed using

Type.GetType().GetProperty()

I need to parse the ProjectDependencies Tab. I want to know if the ProjectDependencies values can be obtained similarly?

Any other solution is also welcome

Thanks in advance!!

回答1:

        var filePath = @"C:\Users\xyz\Documents\Visual Studio 2010\Projects\test\test.sln"
        var slnLines = File.ReadAllLines(filePath);
        var projGuid = new List<string>();
        for(var i=0; i<slnLines.Length; i++)
        {
            if (slnLines[i].Contains(@"ProjectSection(ProjectDependencies)"))
                while (!slnLines[i+1].Contains("EndProjectSection"))
                {
                    var temp = (slnLines[i+1].Split(new[]{'=', '\n', '\t', ' '},2,StringSplitOptions.RemoveEmptyEntries));
                    if(temp.ElementAt(0) == temp.ElementAt(1))
                        projGuid.Add(temp.ElementAt(0));
                    i++;
                }
        }

        using (var file = new StreamWriter(@"C:\Users\xyz\Desktop\Results.txt"))
        {
            foreach (var pGuid in projGuid)
            {
                file.WriteLine(pGuid);
            }
        }

This piece of code does it.

Thanks.