Jiho Han
Jiho Han

Reputation: 1630

How do I get the msbuild path from Visual Studio macro?

I am trying to create a context menu item that will run msbuild on certain extensions. I've found the macro that will check for the extension of the currently selected item in the solution explorer.

All I have left to do is run msbuild on the file. I don't want to hard code the path. So I'm thinking there has to be a way to get the path to msbuild path for the currently loaded solution.

Upvotes: 1

Views: 3607

Answers (1)

Brian Kretzler
Brian Kretzler

Reputation: 9938

Use the reserved property $(MSBuildToolsPath), which will resolve to something like this, depending on the .NET version:

C:\Windows\Microsoft.NET\Frameworks\v4.0.30319\

There is also the $(MSBuildExtensionsPath) family of properties (including 32 and 64-bit specific ones) that will point to:

C:\Program Files\MSBuild\

-- from the book "MSBuild Trickery" tricks #19 and #43

edit: adding macro code to extract

The following Visual Studio Macro will enumerate all of the current properties, including $(MSBuildToolsPath), $(MSBuildToolsRoot) and $(MSBuildToolsPath32), the last one maybe only on a 64-bit machine, I'm not sure.

'
' Note: you need to have an open solution, an active document,
' and a visible Build pane in the Output window
'
Public Module Module1
  Public Sub ListProperties()
    Dim doc As Document = DTE.ActiveDocument
    Dim projectItem As ProjectItem = doc.ProjectItem
    Dim project As Project = projectItem.ContainingProject

    Dim evalProject As Microsoft.Build.Evaluation.Project = _
        Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection _
          .LoadProject(project.FullName)

    Dim ow As EnvDTE.OutputWindow
    ow = DTE.ToolWindows.OutputWindow
    Dim wp As OutputWindowPane
    wp = ow.OutputWindowPanes.Item("Build")

    Dim e As System.Collections.Generic.IEnumerator(Of Microsoft.Build _
      .Evaluation.ProjectProperty)
    e = evalProject.AllEvaluatedProperties.GetEnumerator()
    e.MoveNext()
    For i = 0 To evalProject.AllEvaluatedProperties.Count - 1
      Dim s As String
      s = s + e.Current.Name + " = " + e.Current.UnevaluatedValue + vbCrLf
      wp.OutputString(s)
      e.MoveNext()
      s = ""
    Next
  End Sub
End Module

Upvotes: 3

Related Questions