3

I am trying to get a list of projects and folders under it. I am able to get the projects and project-items using:

DTE2 dte2;
dte2=(DTE2)System.Runtime.InteropServices.Marshal.
GetActiveObject("VisualStudio.DTE.10.0");
Projects projects = dte2.Solution.Projects;

Then, I am iterating through the project items and getting the "kind" of item. But it is showing only GUID. I need to know whether the item is a Folder. How do I do that?

Ref:

var item = projects.GetEnumerator();
while (item.MoveNext())
{
  var project = item.Current as Project;
  for(i=1;i<project.ProjectItems.Count;i++)
  {
     string itemType = project.ProjectItems.Item(i).Kind;
  }
}

EDIT :

Currently, I am using a workaround:

string location = project.ProjectItems.Item(i).get_FileNames(1);
if (location.EndsWith(@"\"))
        {
            // It is a folder E.g C:\\Abc\\Xyz\\
        }
semantic_c0d3r
  • 819
  • 2
  • 15
  • 31

3 Answers3

14

You could use EnvDTE.Constants.vsProjectItemKindPhysicalFolder to compare the ProjectItem.Kind property against.

More constants can be found here: http://msdn.microsoft.com/library/vstudio/envdte.constants

Stefan Born
  • 720
  • 6
  • 14
4

You can use ProjectKinds.vsProjectKindSolutionFolder to see whether the Project is a Folder or not.

e.g.

var item = projects.GetEnumerator();
while (item.MoveNext())
{
  var project = item.Current as Project;
  for(i=1;i<project.ProjectItems.Count;i++)
  {
     string itemType = project.ProjectItems.Item(i).Kind;
     if (itemType  == ProjectKinds.vsProjectKindSolutionFolder)
     {
         // Do whatever
     }
  }
}

EDIT: As mentioned in my comment, I realised after posting that the above is for SolutionFolders which are a Project.Kind rather than a ProjectItem.Kind. Regarding the GUIDS, Microsoft say:

The Kind property of Project or ProjectItem does not return an enum value (since .NET must accommodate project kinds provided by 3rd parties). So, the Kind property returns a unique GUID string to identity the kind. The extensibility model provides some of these GUIDs scattered through several assemblies and classes (EnvDTE.Constants, VSLangProj.PrjKind, VSLangProj2.PrjKind2, etc.) but sometimes you will have to guess the values and hardcode them.

From http://support.microsoft.com/kb/555561. As I said in the comment, hopefully the GUID for a ProjectItem of Kind "Folder" is the same across the board. You just need to determine this GUID and hardcode it.

thudbutt
  • 1,481
  • 1
  • 19
  • 32
  • I've just noticed that the solution I've provided is for SolutionFolders which may have a different GUID from regular folders. However, I would expect regular folders to share the same GUID which, if different from the SolutionFolder GUID, you can use you workaround to obtain and then store as a constant. – thudbutt Jan 12 '13 at 08:16
0

Here is a way to recursively get all folders as a ProjectItem from a Project.

You have to use EnvDTE.Constants.vsProjectItemKindPhysicalFolder against ProjectItem.Kind for project folders rather than solution folders, as mentioned in other answers.

public static List<ProjectItem> GetProjectFolders(EnvDTE.Project project)
{
    var projectFolders = new List<ProjectItem>();

    foreach (ProjectItem projectItem in project.ProjectItems)
    {
        projectFolders.AddRange(GetFolderFromProjectItem(project, projectItem));
    }

    return projectFolders;
}

private static List<ProjectItem> GetFolderFromProjectItem(EnvDTE.Project project, ProjectItem projectItem)
{
     var projectFolders = new List<ProjectItem>();

     if (projectItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder)
     {
          projectFolders.Add(projectItem);

          foreach (ProjectItem subProjectItem in projectItem.ProjectItems)
          {
               projectFolders.AddRange(GetFolderFromProjectItem(project, subProjectItem));
          }
     }

     return projectFoldersRelativePaths;
}
tinyDanza
  • 96
  • 6