Logo
PRB: NotImplementedException adding a solution folder to a solution folder in Visual Studio from a macro or add-in

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2005
Date: January 2011   Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
Introduction

This article discusses a problem that happens when an add-in or macro tries to add a solution folder to a solution folder of the solution using the automation model (EnvDTE) from a Visual Studio macro or add-in.

More Information

Visual Studio 2005 and higher allows you to add folders to the solution (which are called solution folders), not only to add folders to a project (something that was already allowed by Visual Studio .NET 2002). Solution folders can be nested, and a folder that belongs to the solution (a root solution folder) is modeled as an EnvDTE.Project, so to add a child solution folder to a root solution folder you have to use the EnvDTE.Project.ProjectItems.AddFolder method. However, this method causes a NotImplementedException.

The following macro provides the code to reproduce the problem:

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

   Dim objSolution2 As EnvDTE80.Solution2
   Dim objSolutionFolder As EnvDTE.Project
   Dim colKinds As New System.Collections.Generic.List(Of String)
   Dim objChildSolutionFolder As ProjectItem

   objSolution2 = DirectCast(DTE.Solution, EnvDTE80.Solution2)

   ' Create a root solution folder
   objSolutionFolder = objSolution2.AddSolutionFolder("Folder1")

   ' We will try to create a child solution folder with any of these kinds of folder. All of them will fail
   colKinds.Add(EnvDTE.Constants.vsProjectItemKindMisc)
   colKinds.Add(EnvDTE.Constants.vsProjectItemKindPhysicalFolder)
   colKinds.Add(EnvDTE.Constants.vsProjectItemKindSolutionItems)
   colKinds.Add(EnvDTE.Constants.vsProjectItemKindVirtualFolder)
   colKinds.Add(EnvDTE.Constants.vsProjectItemsKindSolutionItems)
   colKinds.Add(EnvDTE.Constants.vsProjectItemKindSubProject)
   colKinds.Add(EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder)

   For Each sKind As String In colKinds

      objChildSolutionFolder = Nothing

      Try
         objChildSolutionFolder = objSolutionFolder.ProjectItems.AddFolder("Folder11", sKind)
      Catch objException As Exception
         MessageBox.Show("Folder of kind " & sKind & " not created because of: " & objException.Message)
      End Try

      If objChildSolutionFolder IsNot Nothing Then
         MessageBox.Show("Folder created of kind " & sKind)
      End If

   Next

End Sub


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


Top