Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

c# - Project Names in Visual Studio Solution sometimes are empty

In visual studio extension I would like to get all projects and their names:

var service = (DTE) Package.GetGlobalService(typeof (SDTE));
var projects = service.Solution.Projects;

foreach (Project project in projects)
//....

This approach work nice and neat for exception of one little problem: project variable returns really exact number of project. But project's full name might be EMPTY if it located in the solution's folder. (I mean the structure of solution when projects are united in the solution's folder)

How to get these project properly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a solution. This guy is the Lord of Rings as minimum: http://www.wwwlicious.com/2011/03/29/envdte-getting-all-projects-html/


Relevant information from the blog, in case it goes away (like so many other answers on Stack Overflow):

DTE2.Solution.Projects will only give you the top level list of items under the Solution so any projects nested within Solution Folders are missed. In order to get at these pesky little worms, you need to burrow into the ProjectItem.SubProject property accessed from the ProjectItems collection on the the Project. Just to cap the whole lot off, if you have nested solution folders, then you need some recursion.

using System.Collections.Generic;  
using EnvDTE;  
using EnvDTE80;

public static class SolutionProjects  
{
    public static DTE2 GetActiveIDE()
    {
        // Get an instance of currently running Visual Studio IDE.
        DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2;
        return dte2;
    }

    public static IList<Project> Projects()
    {
        Projects projects = GetActiveIDE().Solution.Projects;
        List<Project> list = new List<Project>();
        var item = projects.GetEnumerator();
        while (item.MoveNext())
        {
            var project = item.Current as Project;
            if (project == null)
            {
                continue;
            }

            if (project.Kind == ProjectKinds.vsProjectKindSolutionFolder)
            {
                list.AddRange(GetSolutionFolderProjects(project));
            }
            else
            {
                list.Add(project);
            }
        }

        return list;
    }

    private static IEnumerable<Project> GetSolutionFolderProjects(Project solutionFolder)
    {
        List<Project> list = new List<Project>();
        for (var i = 1; i <= solutionFolder.ProjectItems.Count; i++)
        {
            var subProject = solutionFolder.ProjectItems.Item(i).SubProject;
            if (subProject == null)
            {
                continue;
            }

            // If this is another solution folder, do a recursive call, otherwise add
            if (subProject.Kind == ProjectKinds.vsProjectKindSolutionFolder)
            {
               list.AddRange(GetSolutionFolderProjects(subProject));
            }
           else
           {
               list.Add(subProject);
           }
        }
        return list;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...