Visual Studio 解决方案中的项目名称有时为空
Project Names in Visual Studio Solution sometimes are empty
在 visual studio 扩展中,我想获取所有项目及其名称:
var service = (DTE) Package.GetGlobalService(typeof (SDTE));
var projects = service.Solution.Projects;
foreach (Project project in projects)
//....
除了一个小问题外,这种方法工作得很好而且很简洁:
项目变量 returns 确实是项目的确切数量。但是如果项目位于解决方案的文件夹中,它的全名可能是 EMPTY。 (我指的是当项目合并到解决方案的文件夹中时解决方案的结构)
如何正确获取这些项目?
这是一个解决方案。
这家伙至少是指环王:
http://www.wwwlicious.com/2011/03/29/envdte-getting-all-projects-html/
来自博客的相关信息,以防它消失(就像 Stack Overflow 上的许多其他答案一样):
DTE2.Solution.Projects will only give you the top level list of items under the Solution so any projects nested within Solution Folders are missed. In order to get at these pesky little worms, you need to burrow into the ProjectItem.SubProject property accessed from the ProjectItems collection on the the Project. Just to cap the whole lot off, if you have nested solution folders, then you need some recursion.
using System.Collections.Generic;
using EnvDTE;
using EnvDTE80;
public static class SolutionProjects
{
public static DTE2 GetActiveIDE()
{
// Get an instance of currently running Visual Studio IDE.
DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2;
return dte2;
}
public static IList<Project> Projects()
{
Projects projects = GetActiveIDE().Solution.Projects;
List<Project> list = new List<Project>();
var item = projects.GetEnumerator();
while (item.MoveNext())
{
var project = item.Current as Project;
if (project == null)
{
continue;
}
if (project.Kind == ProjectKinds.vsProjectKindSolutionFolder)
{
list.AddRange(GetSolutionFolderProjects(project));
}
else
{
list.Add(project);
}
}
return list;
}
private static IEnumerable<Project> GetSolutionFolderProjects(Project solutionFolder)
{
List<Project> list = new List<Project>();
for (var i = 1; i <= solutionFolder.ProjectItems.Count; i++)
{
var subProject = solutionFolder.ProjectItems.Item(i).SubProject;
if (subProject == null)
{
continue;
}
// If this is another solution folder, do a recursive call, otherwise add
if (subProject.Kind == ProjectKinds.vsProjectKindSolutionFolder)
{
list.AddRange(GetSolutionFolderProjects(subProject));
}
else
{
list.Add(subProject);
}
}
return list;
}
}
在 visual studio 扩展中,我想获取所有项目及其名称:
var service = (DTE) Package.GetGlobalService(typeof (SDTE));
var projects = service.Solution.Projects;
foreach (Project project in projects)
//....
除了一个小问题外,这种方法工作得很好而且很简洁: 项目变量 returns 确实是项目的确切数量。但是如果项目位于解决方案的文件夹中,它的全名可能是 EMPTY。 (我指的是当项目合并到解决方案的文件夹中时解决方案的结构)
如何正确获取这些项目?
这是一个解决方案。 这家伙至少是指环王: http://www.wwwlicious.com/2011/03/29/envdte-getting-all-projects-html/
来自博客的相关信息,以防它消失(就像 Stack Overflow 上的许多其他答案一样):
DTE2.Solution.Projects will only give you the top level list of items under the Solution so any projects nested within Solution Folders are missed. In order to get at these pesky little worms, you need to burrow into the ProjectItem.SubProject property accessed from the ProjectItems collection on the the Project. Just to cap the whole lot off, if you have nested solution folders, then you need some recursion.
using System.Collections.Generic;
using EnvDTE;
using EnvDTE80;
public static class SolutionProjects
{
public static DTE2 GetActiveIDE()
{
// Get an instance of currently running Visual Studio IDE.
DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2;
return dte2;
}
public static IList<Project> Projects()
{
Projects projects = GetActiveIDE().Solution.Projects;
List<Project> list = new List<Project>();
var item = projects.GetEnumerator();
while (item.MoveNext())
{
var project = item.Current as Project;
if (project == null)
{
continue;
}
if (project.Kind == ProjectKinds.vsProjectKindSolutionFolder)
{
list.AddRange(GetSolutionFolderProjects(project));
}
else
{
list.Add(project);
}
}
return list;
}
private static IEnumerable<Project> GetSolutionFolderProjects(Project solutionFolder)
{
List<Project> list = new List<Project>();
for (var i = 1; i <= solutionFolder.ProjectItems.Count; i++)
{
var subProject = solutionFolder.ProjectItems.Item(i).SubProject;
if (subProject == null)
{
continue;
}
// If this is another solution folder, do a recursive call, otherwise add
if (subProject.Kind == ProjectKinds.vsProjectKindSolutionFolder)
{
list.AddRange(GetSolutionFolderProjects(subProject));
}
else
{
list.Add(subProject);
}
}
return list;
}
}