How to get folders under projects?

2019-02-18 19:49发布

问题:

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\\
        }

回答1:

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



回答2:

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.



标签: c# envdte