2

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Mrchief
  • 75,126
  • 20
  • 142
  • 189

3 Answers3

10

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.

ckittel
  • 6,478
  • 3
  • 41
  • 71
  • This works flawlessly (its kind of a hack though)!! I would leave this open in case someone can find a better solution otherwise this will be the accepted answer. – Mrchief Aug 02 '11 at 14:28
  • @Mrchief Glad to hear. I wonder if it's not so much of a hack, but simply unfortunate naming? It looks like the API for these macros haven't changed significantly with the various VS updates, probably to prevent unnecessary backward compatibility breakage. IIRC Visual Studio 2003 didn't have the concept of solution folders, or nested projects. So maybe back then the naming was perfect, but now not so much. – ckittel Aug 02 '11 at 15:09
  • I see where you going but IMHO, this whole extensibility thing with backward compatibility is kludgy at best. They could have introduced a flag/property when they introduced the idea of solution folders. Having them list as Projects is a hack in itself (sounds like a last minute addition). – Mrchief Aug 02 '11 at 15:15
2

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

Brian Schmitt
  • 6,008
  • 1
  • 24
  • 36
0

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