Search code examples
vb.netvisual-studio-2012visual-studio-addinsvsx

VSX getting current project thats opened when using addin Exec


I am currently just seeing how to go about making add-in for Visual Studio 2012. I currently have a button on the toolbar than when clicked opens a winform.

Once I start to debug the add-in it opens another instance of VS2012 and I then open up a random project that I was working on.

Once I open that random project, I then click on the add-in button and it pops up the winform with no problems. However, I am having issues with trying to figure out how to get the current projects name, path, etc that I am working on in order to have the add-in do stuff for me for that randomly opened project.

The code I have to open up the popup on the add-in button click:

Public Sub Exec(ByVal cmdName As String, ByVal executeOption As vsCommandExecOption, ByRef varIn As Object, ByRef varOut As Object, ByRef handled As Boolean) Implements IDTCommandTarget.Exec
    handled = False

    If (executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault) Then
        If cmdName = addInInstance.ProgID & "." & MY_COMMAND_NAME Then
            Dim theForm As mainForm = New mainForm(addInInstance)

            handled = True
            theForm.ShowDialog()
        End If
    End If
End Sub

Where _applicationObject is defined and set:

Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
    Try
        applicationObject = CType(application, EnvDTE.DTE)
        addInInstance = CType(addInInst, EnvDTE.AddIn)
    .....
End Sub

And the winform code looks like this:

Public Sub New(ByVal _applicationObject As EnvDTE.AddIn)
    InitializeComponent()
    Dim solutionDir As String = System.IO.Path.GetDirectoryName(_applicationObject.Name)
End Sub

I would figure it would be possible for a VS add-in to know what project was currently opened in the IDE and gather information about it? Currently the winform code shows "" for siolutionDir but _applicationObject.Name has a value of SourceCC - No Name provided. which is what my add-in is called SourceCC.

Any help would be great as this is my first add-in for VS!


Solution

  • In the OnConnection method you get the applicationObject that has the Solution property of type EnvDTE.Solution. You can use applicationObject.Solution to access the currently opened solution.