C# 如何获取解决方案中的所有项目名称?

C# 如何获取解决方案中的所有项目名称?,c#,visual-studio-2012,C#,Visual Studio 2012,如何通过C#获取解决方案中的所有项目名称? 该解决方案具有控制台项目,其中有许多windows服务 我的目标是找到所有项目的名称,然后获取项目中所有windows服务的名称 该实现将在同一解决方案的新项目中完成。 感谢您的提示。请检查此选项,以解决您的问题 .sln文件包含环境用于 查找并加载持久化数据和 它引用的项目包。当用户打开解决方案时 环境在预解、项目和后解中循环 用于加载解决方案的.sln文件中的信息,以及 解决方案,以及附加到解决方案的任何持久化信息 也检查一下 也请检查此项(约翰·

如何通过C#获取解决方案中的所有项目名称? 该解决方案具有控制台项目,其中有许多windows服务

我的目标是找到所有项目的名称,然后获取项目中所有windows服务的名称

该实现将在同一解决方案的新项目中完成。 感谢您的提示。

请检查此选项,以解决您的问题

.sln文件包含环境用于 查找并加载持久化数据和 它引用的项目包。当用户打开解决方案时 环境在预解、项目和后解中循环 用于加载解决方案的.sln文件中的信息,以及 解决方案,以及附加到解决方案的任何持久化信息

也检查一下

也请检查此项(约翰·莱德伦提供了如此好的答案,这要归功于他)

公共类解决方案
{
//内部类解决方案解析器
//名称:Microsoft.Build.Construction.SolutionParser
//程序集:Microsoft.Build,版本=4.0.0.0
静态只读类型s_SolutionParser;
静态只读属性信息s_SolutionParser_solutionReader;
静态只读MethodInfo s_SolutionParser_parseSolution;
静态只读属性信息s_SolutionParser_项目;
静态溶液()
{
s_SolutionParser=Type.GetType(“Microsoft.Build.Construction.SolutionParser,Microsoft.Build,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”,false,false);
如果(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);
}
}
公共列表项目{get;private set;}
公共解决方案(字符串解决方案文件名)
{
如果(s_SolutionParser==null)
{
抛出新的InvalidOperationException(“找不到类型“Microsoft.Build.Construction.SolutionParser”是否缺少对“Microsoft.Build.dll”的程序集引用?”;
}
var solutionParser=s_solutionParser.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First().Invoke(null);
使用(var streamReader=newstreamreader(solutionFileName))
{
s_SolutionParser_solutionReader.SetValue(SolutionParser,streamReader,null);
s_SolutionParser_parseSolution.Invoke(SolutionParser,null);
}
var projects=新列表();
var array=(array)s_SolutionParser_projects.GetValue(SolutionParser,null);
for(int i=0;i
使用新的API会更容易


要使用线程的解决方案,我必须使用Microsoft.VisualStudio.Shell.Interop。我在Visual Studio 2012中找不到它。它在Visual Studio 2012中。检查:-为什么在你自己的答案中重新发布。。。即使你给了信用。我正在尝试使用解决方案文件获取我所有csproj的AssemblyName。这让我震惊。下面是获取每个“SolutionProject”的完整路径的代码。System.IO.Path.Combine(System.IO.Path.GetDirectoryName(solutionFileName),currentSolutionProject.RelativePath);
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 = 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 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;
    }
}

[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 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);
        }
    }

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

    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;
    }
}
var workspace=MSBuildWorkspace.Create();
var solution = workspace.OpenSolutionAsync(solutionPath).Result;
var projects = solution.Projects;
foreach(var project in projects)
{
  //TODO              
}