如何从Visual Studio宏获取msbuild路径?

时间:2011-10-20 19:09:57

标签: .net visual-studio visual-studio-2008 msbuild

我正在尝试创建一个上下文菜单项,它将在某些扩展上运行msbuild。 我找到了将检查解决方案资源管理器中当前所选项目的扩展名的宏。

我要做的就是在文件上运行msbuild。我不想硬编码路径。 所以我认为必须有一种方法来获取当前加载的解决方案的msbuild路径。

1 个答案:

答案 0 :(得分:3)

使用保留属性$(MSBuildToolsPath),它将解析为类似的内容,具体取决于.NET版本:

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

还有$(MSBuildExtensionsPath)属性系列(包括32位和64位特定属性)将指向:

C:\Program Files\MSBuild\

- 来自“MSBuild Trickery”一书的技巧#19和#43

编辑:添加宏代码以提取

以下Visual Studio宏将枚举所有当前属性,包括$(MSBuildToolsPath),$(MSBuildToolsRoot)和$(MSBuildToolsPath32),最后一个可能只在64位计算机上,我不确定。

'
' 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