Sln file parsing C #

Original: C # sln file parsing

My project, coding tools necessary to detect open a project, get all the items. But I found that the method, if there is a folder, the folder on the project, then the project is not available, so I looked for a way to get all the items sln files.

The method previously used dte.Solution.Projectsbut on the project folder not obtain, so use the stack to offer.

First, add a reference Microsoft.Buildnote version

Write pictures described here

Then I three classes into the project, in fact, put two just fine details, see my github

    public class Solution
    {
        //internal class SolutionParser
        //Name: Microsoft.Build.Construction.SolutionParser
        //Assembly: Microsoft.Build, Version=4.0.0.0

        static readonly Type s_SolutionParser;
        static readonly PropertyInfo s_SolutionParser_solutionReader;
        static readonly MethodInfo s_SolutionParser_parseSolution;
        static readonly PropertyInfo s_SolutionParser_projects;

        static Solution()
        {
            //s_SolutionParser_projects.GetValue()
            s_SolutionParser = Type.GetType("Microsoft.Build.Construction.SolutionParser, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
            if (s_SolutionParser != null)
            {
                s_SolutionParser_solutionReader = s_SolutionParser.GetProperty("SolutionReader", BindingFlags.NonPublic | BindingFlags.Instance);
                s_SolutionParser_projects = s_SolutionParser.GetProperty("Projects", BindingFlags.NonPublic | BindingFlags.Instance);
                s_SolutionParser_parseSolution = s_SolutionParser.GetMethod("ParseSolution", BindingFlags.NonPublic | BindingFlags.Instance);
            }
        }

        public List<SolutionProject> Projects { get; private set; }
        public List<SolutionConfiguration> Configurations { get; private set; }

        public Solution(string solutionFileName)
        {
            if (s_SolutionParser == null)
            {
                throw new InvalidOperationException("Can not find type 'Microsoft.Build.Construction.SolutionParser' are you missing a assembly reference to 'Microsoft.Build.dll'?");
            }
            var solutionParser = s_SolutionParser.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First().Invoke(null);
            using (var streamReader = new StreamReader(solutionFileName))
            {
                s_SolutionParser_solutionReader.SetValue(solutionParser, streamReader, null);
                s_SolutionParser_parseSolution.Invoke(solutionParser, null);
            }
            var projects = new List<SolutionProject>();
            var array = (Array)s_SolutionParser_projects.GetValue(solutionParser, null);
            for (int i = 0; i < array.Length; i++)
            {
                projects.Add(new SolutionProject(array.GetValue(i)));
            }
            this.Projects = projects;
            GetProjectFullName(solutionFileName);
            //Object cfgArray = //s_SolutionParser_configurations.GetValue
            //    s_SolutionParser_projects.GetValue(solutionParser, null);
            //PropertyInfo[] pInfos = null;
            //pInfos = cfgArray.GetType().GetProperties();
            //int count = (int)pInfos[1].GetValue(cfgArray, null);

            //var configs = new List<SolutionConfiguration>();
            //for (int i = 0; i < count; i++)
            //{
            //    configs.Add(new SolutionConfiguration(pInfos[2].GetValue(cfgArray, new object[] { i })));
            //}

            //this.Configurations = configs;
        }

        private void GetProjectFullName(string solutionFileName)
        {
            DirectoryInfo solution = (new FileInfo(solutionFileName)).Directory;
            foreach (var temp in Projects.Where
                //(temp=>temp.RelativePath.EndsWith("csproj"))
                (temp => !temp.RelativePath.Equals(temp.ProjectName))
            )
            {
                GetProjectFullName(solution, temp);
            }
        }

        private void GetProjectFullName(DirectoryInfo solution, SolutionProject project)
        {
            //Uri newUri =new Uri(,UriKind./*Absolute*/);
            //if(project.RelativePath)

            project.FullName = System.IO.Path.Combine(solution.FullName, project.RelativePath);
        }
    }

    [DebuggerDisplay("{ProjectName}, {RelativePath}, {ProjectGuid}")]
    public class SolutionProject
    {
        static readonly Type s_ProjectInSolution;
        static readonly PropertyInfo s_ProjectInSolution_ProjectName;
        static readonly PropertyInfo s_ProjectInSolution_RelativePath;
        static readonly PropertyInfo s_ProjectInSolution_ProjectGuid;
        static readonly PropertyInfo s_ProjectInSolution_ProjectType;

        static SolutionProject()
        {
            s_ProjectInSolution = Type.GetType("Microsoft.Build.Construction.ProjectInSolution, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
            if (s_ProjectInSolution != null)
            {
                s_ProjectInSolution_ProjectName = s_ProjectInSolution.GetProperty("ProjectName", BindingFlags.NonPublic | BindingFlags.Instance);
                s_ProjectInSolution_RelativePath = s_ProjectInSolution.GetProperty("RelativePath", BindingFlags.NonPublic | BindingFlags.Instance);
                s_ProjectInSolution_ProjectGuid = s_ProjectInSolution.GetProperty("ProjectGuid", BindingFlags.NonPublic | BindingFlags.Instance);
                s_ProjectInSolution_ProjectType = s_ProjectInSolution.GetProperty("ProjectType", BindingFlags.NonPublic | BindingFlags.Instance);
            }
        }

        public string ProjectName { get; private set; }
        public string RelativePath { get; private set; }
        public string ProjectGuid { get; private set; }
        public string ProjectType { get; private set; }
        public string FullName { set; get; }

        public SolutionProject(object solutionProject)
        {
            this.ProjectName = s_ProjectInSolution_ProjectName.GetValue(solutionProject, null) as string;
            this.RelativePath = s_ProjectInSolution_RelativePath.GetValue(solutionProject, null) as string;
            this.ProjectGuid = s_ProjectInSolution_ProjectGuid.GetValue(solutionProject, null) as string;
            this.ProjectType = s_ProjectInSolution_ProjectType.GetValue(solutionProject, null).ToString();
        }
    }

    public class SolutionConfiguration
    {
        static readonly Type s_ConfigInSolution;
        static readonly PropertyInfo configInSolution_configurationname;
        static readonly PropertyInfo configInSolution_fullName;
        static readonly PropertyInfo configInSolution_platformName;

        static SolutionConfiguration()
        {
            s_ConfigInSolution = Type.GetType("Microsoft.Build.Construction.ConfigurationInSolution, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
            if (s_ConfigInSolution != null)
            {
                configInSolution_configurationname = s_ConfigInSolution.GetProperty("ConfigurationName", BindingFlags.NonPublic | BindingFlags.Instance);
                configInSolution_fullName = s_ConfigInSolution.GetProperty("FullName", BindingFlags.NonPublic | BindingFlags.Instance);
                configInSolution_platformName = s_ConfigInSolution.GetProperty("PlatformName", BindingFlags.NonPublic | BindingFlags.Instance);
            }
        }

        public string configurationName { get; private set; }
        public string fullName { get; private set; }
        public string platformName { get; private set; }


        public SolutionConfiguration(object solutionConfiguration)
        {
            this.configurationName = configInSolution_configurationname.GetValue(solutionConfiguration, null) as string;
            this.fullName = configInSolution_fullName.GetValue(solutionConfiguration, null) as string;
            this.platformName = configInSolution_platformName.GetValue(solutionConfiguration, null) as string;
        }
    }

Note To quote

     using System;
     using System.Collections.Generic;
     using System.Diagnostics;
     using System.IO;
     using System.Linq;
     using System.Reflection;

Say a little above the code used is primarily reflected.

Sln get resolved with the reflection s_SolutionParser_parseSolutionthat he can get all the items.

But the project obtained path is relative, then use C # turn relative path absolute path , the path can be converted project.

use

Enter the project file name like, enter a project name will automatically get all the items.

  Solution solution = new Solution(工程文件路径);

Get the project files for all projects

  foreach (var temp in solution.Projects)
  {


  }

Code: https://gist.github.com/lindexi/b36feb816fe9e586ffbbdf58397b25da

See also: https://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.project.propertygroups%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

https://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.project%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

http://stackoverflow.com/questions/707107/parsing-visual-studio-solution-files


This article will be updated frequently, please read the original text: https://blog.lindexi.com/post/C-%E8%A7%A3%E6%9E%90-sln-%E6%96%87%E4%BB%B6 .html , in order to avoid misleading the old error of knowledge, and a better reading experience.

If you want to continue to read my latest blog, please click on the RSS feed , it is recommended to use RSS Stalker subscribe to blog, or visit my homepage CSDN concern

Creative Commons License This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing. Welcome to reprint, use, repost, but be sure to keep the article signed by Linde Xi (containing links: https://blog.lindexi.com ), shall not be used for commercial purposes, be sure to publish the same work based on the paper license modification. If you have any questions, please contact me .

The following are advertising time

Recommended public concern Edi.Wang No.

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12082258.html