如何在项目下获取文件夹?

时间:2013-01-11 06:15:48

标签: c# envdte

我正在尝试获取其下的项目和文件夹列表。我可以使用以下方式获取项目和项目项目:

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

然后,我正在遍历项目项目并获得项目的“种类”。但它只显示GUID。我需要知道该项目是否是文件夹。我该怎么做?

价:

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

编辑:

目前,我正在使用解决方法:

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

2 个答案:

答案 0 :(得分:13)

您可以使用EnvDTE.Constants.vsProjectItemKindPhysicalFolder来比较ProjectItem.Kind属性。

更多常量可以在这里找到:http://msdn.microsoft.com/library/vstudio/envdte.constants

答案 1 :(得分:4)

您可以使用ProjectKinds.vsProjectKindSolutionFolder查看项目是否为文件夹。

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
     }
  }
}
编辑:正如我的评论中所提到的,我在发布之后意识到上面是针对SolutionFolders而不是ProjectItem.Kind。关于GUIDS,微软说:

  

Project或ProjectItem的Kind属性不返回枚举值(因为.NET必须包含第三方提供的项目类型)。因此,Kind属性返回唯一的GUID字符串以标识该类型。可扩展性模型提供了一些分散在几个程序集和类中的GUID(EnvDTE.Constants,VSLangProj.PrjKind,VSLangProj2.PrjKind2等),但有时你必须猜测它们并对它们进行硬编码。

来自http://support.microsoft.com/kb/555561。正如我在评论中所说的那样,希望ProjectItem of Kind“文件夹”的GUID是全面的。您只需要确定此GUID并对其进行硬编码。

相关问题