Mrchief
Mrchief

Reputation: 76248

Visual Studio Macro to list Projects instead of solution folders

Using the following code gives me Solution folders instead of real projects.

projectName = DTE.Solution.SolutionBuild.StartupProjects(0)
For Each project In DTE.Solution.Projects
    If project.UniqueName = projectName Then
        Return project
    End If
Next

Is there way I can loop through actual Project nodes?

I'm trying to read properties from the startup project.

Upvotes: 2

Views: 2812

Answers (3)

Saravana Kumar
Saravana Kumar

Reputation: 9

To Get the Project from Solution folder, use the property ProjectItem.SubProject

Upvotes: 0

Brian Schmitt
Brian Schmitt

Reputation: 6068

I think you may want to check against the VS constants, try the following:

Private Function GetAllProjects() As Generic.List(Of Project)
    Dim lst As New Generic.List(Of Project)
    For Each proj As Project In DTE.Solution.Projects
    If proj.Kind = Constants.vsProjectKindSolutionItems Then
        lst.AddRange(GetSubProjects(proj.ProjectItems))
    Else
        lst.Add(proj)
    End If
    Next
    Return lst
End Function

Private Function GetSubProjects(ByVal pis As ProjectItems) As Generic.List(Of Project)
    Dim lst As New Generic.List(Of Project)
    For Each pi As ProjectItem In pis
    If pi.Kind = Constants.vsProjectItemKindSolutionItems Then
        lst.Add(pi.SubProject)
    ElseIf pi.Kind = Constants.vsProjectKindSolutionItems Then
        lst.AddRange(GetSubProjects(pi.ProjectItems))
    End If
    Next
    Return lst
End Function

Its part of a larger macro from my blog at http://www.brianschmitt.com/2009/10/fixing-visual-studio-add-reference.html

Upvotes: 2

ckittel
ckittel

Reputation: 6646

I've never written any Visual Studio macros, but this may be what you are looking for.

projectName = DTE.Solution.SolutionBuild.StartupProjects(0)
For Each project In DTE.Solution.Projects
    If (project.ConfigurationManager IsNot Nothing) Then
        ' It's a project!
        If (project.UniqueName = projectName) Then Return project
    Else
        If (project.ProjectItems IsNot Nothing) Then
            For Each projectItem In project.ProjectItems
                If (projectItem.SubProject IsNot Nothing) Then
                   ' TODO: Recurse on projectItem.SubProject 
                End If
            Next
        End If
    End If
Next

I left a 'TODO in there, because you would need to actually pull this out into a function that you could recursively call if you are looking to deal with nested (sub) projects.

I got this solution from this link, and while it's Visual Studio 2005-era material, it might get you going in the right direction.

Upvotes: 10

Related Questions