Parse ProjectDependency from .sln file

六月ゝ 毕业季﹏ 提交于 2019-12-11 07:18:06

问题


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.



来源:https://stackoverflow.com/questions/12308040/parse-projectdependency-from-sln-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!