Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Project information from EnvDTE at Design time

How-to

Get Project information from EnvDTE at Design time

by  SBendBuckeye  Posted    (Edited  )
Hello all,

I was working on some code and needed to access Project information at design time. It was a real struggle to find any documentation on how to do that. Below is a method I wrote which help me get started.

A. I put this code inside a Designer class because I needed to use GetService and Component.Site provides a GetService method

B. You will need to set a reference to envdte for the code to work.
Code:
Public Overrides Sub Initialize(ByVal component As System.ComponentModel.IComponent)
    Try
        Dim visualStudioIDE As EnvDTE.DTE = _
            CType(component.Site.GetService(GetType(EnvDTE.DTE)), EnvDTE.DTE)
        If visualStudioIDE Is Nothing Then Throw New InvalidOperationException("DTE not found.")
        Dim o, projects, mainWindow As Object
        Dim ad As EnvDTE.Document
        Dim aw As EnvDTE.Window
        Dim docs As EnvDTE.Documents
        Dim mode As EnvDTE.vsIDEMode
        Dim solution As EnvDTE.Solution
        Dim windows As EnvDTE.Windows
        With visualStudioIDE
            ad = .ActiveDocument
            projects = .ActiveSolutionProjects
            aw = .ActiveWindow
            docs = .Documents
            o = .GetObject("VBProjects")
            mode = .Mode
            mainWindow = .MainWindow
            solution = .Solution
            windows = .Windows
        End With

        Dim text As String
        For Each [property] As EnvDTE.Property In aw.Project.Properties
            text &= [property].Name & " " & [property].Value.ToString & vbNewLine
        Next
        MsgBox(text, MsgBoxStyle.Information)
        Console.WriteLine("")
        Console.WriteLine(text)

        text = String.Empty
        For Each item As EnvDTE.ProjectItem In aw.Project.ProjectItems
            text &= item.Name & " " & item.FileNames(0) & vbNewLine
        Next
        MsgBox(text, MsgBoxStyle.Information)
        Console.WriteLine("")
        Console.WriteLine(text)

    Catch ex As Exception
        Throw
    End Try
End Sub 'Initialize
Enjoy...
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top