HOWTO: Get the output build folder from a Visual Studio add-in or macro

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: July 2009   Microsoft Visual Studio .NET 2003
Updated: March 2013   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012


A Visual Studio project can have several project configurations, each one with a different output build folder that is set in the project properties window, Compile/Build section, Build output path. Furthermore, the output build folder can be absolute or relative to the project folder.

More Information

The following macro gets the absolute output build folder for the active configuration of each project of a solution.

Language: VB.NET   Copy Code Copy Code (IE only)
Sub Macro1()

   Dim proj As EnvDTE.Project

   For Each proj In DTE.Solution.Projects



End Sub

Sub GetProjectOutputBuildFolder(ByVal proj As EnvDTE.Project)

   Dim activeConfiguration As EnvDTE.Configuration
   Dim configManager As EnvDTE.ConfigurationManager
   Dim outputPath As String
   Dim absoluteOutputPath As String
   Dim projectFolder As String


      ' Get the configuration manager of the project
      configManager = proj.ConfigurationManager

      If configManager Is Nothing Then

         MessageBox.Show("The project " & proj.Name & " doesn't have a configuration manager")


         ' Get the active project configuration
         activeConfiguration = configManager.ActiveConfiguration

         ' Get the output folder
         outputPath = activeConfiguration.Properties.Item("OutputPath").Value.ToString()

         ' The output folder can have these patterns:
         ' 1) "\\server\folder"
         ' 2) "drive:\folder"
         ' 3) "..\..\folder"
         ' 4) "folder"

         If outputPath.StartsWith(IO.Path.DirectorySeparatorChar & IO.Path.DirectorySeparatorChar) Then

            ' This is the case 1: "\\server\folder"
            absoluteOutputPath = outputPath

         ElseIf outputPath.Length >= 2 AndAlso outputPath.Chars(1) = IO.Path.VolumeSeparatorChar Then

            ' This is the case 2: "drive:\folder"
            absoluteOutputPath = outputPath

         ElseIf outputPath.IndexOf("..\") <> -1 Then

            ' This is the case 3: "..\..\folder"
            projectFolder = IO.Path.GetDirectoryName(proj.FullName)

            Do While outputPath.StartsWith("..\")
               outputPath = outputPath.Substring(3)
               projectFolder = IO.Path.GetDirectoryName(projectFolder)

            absoluteOutputPath = IO.Path.Combine(projectFolder, outputPath)


            ' This is the case 4: "folder"
            projectFolder = IO.Path.GetDirectoryName(proj.FullName)
            absoluteOutputPath = IO.Path.Combine(projectFolder, outputPath)

         End If

         MessageBox.Show("Output folder of " & proj.Name & ": " & absoluteOutputPath)

      End If

   Catch ex As Exception


   End Try

End Sub

Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)