如何在一个解决方案中获取所有项目的名称?

9

如何通过C#获取解决方案中所有项目的名称? 解决方案中有许多包含Windows服务的控制台项目。

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

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

2个回答

6

查看此MSDN,它将解决您的问题。

.sln文件包含基于文本的信息,环境使用该信息来查找和加载持久数据的名称值参数以及它引用的项目VSPackages。当用户打开解决方案时,环境会在.sln文件中循环遍历preSolution、Project和postSolution信息,以加载解决方案、解决方案内的项目以及附加到解决方案的任何持久化信息。

还要查看EnvDTE:获取所有项目

还要查看此线程(感谢John Leidegren提供了这么好的答案)

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

要使用线程的解决方案,我必须使用Microsoft.VisualStudio.Shell.Interop。但是我在Visual Studio 2012中找不到它。 - user1108948
它在Visual Studio 2012中存在。请查看此链接:http://msdn.microsoft.com/zh-cn/library/bb164686.aspx - Rahul Tripathi
为什么要在自己的答案中转载另一个StackOverflow答案...即使你给出了来源。 - MikeSmithDev
我正在尝试使用解决方案文件获取所有csproj的AssemblyName(s)。这真是太棒了。以下是获取每个“SolutionProject”完整路径的代码。System.IO.Path.Combine(System.IO.Path.GetDirectoryName(solutionFileName), currentSolutionProject.RelativePath); - granadaCoder

5

使用新的Roslyn API会更容易

var workspace=MSBuildWorkspace.Create();
var solution = workspace.OpenSolutionAsync(solutionPath).Result;
var projects = solution.Projects;
foreach(var project in projects)
{
  //TODO              
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接