Hello, Couple days ago, I asked one question about customized project within solution folder. Today I have another question about customized project within Solution Folder: How to iterate projects (customized project type) within solution folder? I know I can iterate projecs in a solution by following code:
public static Project[] IterateCustomProjects(EnvDTE.DTE dte)
{
List<Project> projects = new List<Project>();
foreach (Project project in dte.Solution.Projects)
{
if (project.Kind.Equals(EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder, StringComparison.OrdinalIgnoreCase))
{
projects.AddRange(IterateCustomProjects(project.ProjectItems));
}
else if (project.Kind.Equals(Constants.Constants.PrjKindMyProject, StringComparison.OrdinalIgnoreCase))
{
projects.Add(project);
}
}
return projects.ToArray();
}
public static Project[] IterateCustomProjects(ProjectItems projectItems)
{
List<Project> projects = new List<Project>();
foreach (ProjectItem item in projectItems)
{
Project project = item.SubProject;
if (project == null)
{
continue;
}
else if (project.Kind.Equals(EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder, StringComparison.OrdinalIgnoreCase))
{
projects.AddRange(IterateCustomProjects(project.ProjectItems));
}
else if (project.Kind.Equals(Constants.Constants.PrjKindMyProject, StringComparison.OrdinalIgnoreCase))
{
projects.Add(project);
}
}
return projects.ToArray();
}
But it doesn't work for customized project type.Becauseitem.SubProject of customized project is alwals null. Is there another way to iterate (customized) projects in a solution? Thanks, Avery | | Avery_wu Monday, July 27, 2009 8:28 AM | Finally, I found a solution to find specified type Projects in solution by using the following code:
public static Project[] GetProjectsFromSolution(IVsSolution solution, string projectKind)
{
List<Project> projects = new List<Project>();
if (solution == null)
{
return projects.ToArray();
}
IEnumHierarchies ppEnum;
Guid tempGuid = Guid.Empty;
solution.GetProjectEnum((uint)Microsoft.VisualStudio.Shell.Interop.__VSENUMPROJFLAGS.EPF_ALLPROJECTS, ref tempGuid, out ppEnum);
if (ppEnum != null)
{
uint actualResult = 0;
IVsHierarchy[] nodes = new IVsHierarchy[1];
while (0 == ppEnum.Next(1, nodes, out actualResult))
{
Object obj;
nodes[0].GetProperty((uint)Microsoft.VisualStudio.VSConstants.VSITEMID_ROOT, (int)Microsoft.VisualStudio.Shell.Interop.__VSHPROPID.VSHPROPID_ExtObject, out obj);
Project project = obj as Project;
if (project != null)
{
if (string.IsNullOrEmpty(projectKind))
{
projects.Add(project);
}
else if (projectKind.Equals(project.Kind, StringComparison.InvariantCultureIgnoreCase))
{
projects.Add(project);
}
}
}
}
return projects.ToArray();
}
For IVsSolution interface, we can get it in this way:
IVsSolution vs = GetService(typeof(SVsSolution)) as IVsSolution;
Thanks, Avery - Marked As Answer byAvery_wu Wednesday, July 29, 2009 12:02 PM
-
| | Avery_wu Wednesday, July 29, 2009 12:01 PM | Hello , Maybe you misunderstood about the ProjectItem.SubProject property. You can reference http://msdn.microsoft.com/en-us/library/bb165055(VS.80).aspxto gain information about SubProject. I modified you code and it can iterate allthe project of the solutionon my PC
public static Project[] IterateCustomProjects(EnvDTE80.DTE2 dte)
{
List<Project> projects = new List<Project>();
foreach (Project project in dte.Solution.Projects)
{
if (project.Kind.Equals(EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder, StringComparison.OrdinalIgnoreCase))
{
projects.AddRange(IterateCustomProjects(project.ProjectItems));
}
else
{
projects.Add(project);
}
}
return projects.ToArray();
}
public static Project[] IterateCustomProjects(ProjectItems projectItems)
{
List<Project> projects = new List<Project>();
try
{
foreach (ProjectItem item in projectItems)
{
if (item.Kind.Equals(EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder, StringComparison.OrdinalIgnoreCase))
{
projects.AddRange(IterateCustomProjects(item.ProjectItems));
}
else
{
if(item.Object is Project)
{
projects.Add((Project)item.Object);
}
}
}
}
catch (Exception ex)
{
}
return projects.ToArray();
}
| | Kakuchyou Wednesday, July 29, 2009 10:24 AM | Hi Avery, I am not sure you understand ProjectItem.SubProject clearly: If the project item is the root of a subproject, then the SubProject property returns the Project object for the subproject. Do you want to get the Projects under Solution Folder or get all of the Projects including nested projects? If you want to get all the projects including nested projects,it need to callSubProject property, but I think it will get customized projects if you load package correctly. Socould you give a reproduce sample here? Best Regards, Nancy
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. | | Nancy Shao Wednesday, July 29, 2009 11:36 AM | Thanks,Jim.
Actually, for customized project under solution folder, item.Object is also null. Your code still doesn't work.
Thanks, Avery | | Avery_wu Wednesday, July 29, 2009 11:45 AM | Hi Nancy,
The following is the hierachy in solution explorer I want to iterate.
- Solution1
. SolutionFolder1 . MyProject1.myproj . SolutionFolder2 . MyProject2.myproj
SolutionFolder1, SolutionFolder2 are Visual Studio Build-In solution folder. MyProject1.myproj, MyProject2.myproj are customized project by using MPF. I want to iterate the whole solution to find all of Projects which type is "myproj". But the code attached in my original threaddidn't work. About customized project to reproduce the isuue, you can use IronPython sample within Visual Studio SDK.
Thanks, Avery | | Avery_wu Wednesday, July 29, 2009 11:56 AM | Finally, I found a solution to find specified type Projects in solution by using the following code:
public static Project[] GetProjectsFromSolution(IVsSolution solution, string projectKind)
{
List<Project> projects = new List<Project>();
if (solution == null)
{
return projects.ToArray();
}
IEnumHierarchies ppEnum;
Guid tempGuid = Guid.Empty;
solution.GetProjectEnum((uint)Microsoft.VisualStudio.Shell.Interop.__VSENUMPROJFLAGS.EPF_ALLPROJECTS, ref tempGuid, out ppEnum);
if (ppEnum != null)
{
uint actualResult = 0;
IVsHierarchy[] nodes = new IVsHierarchy[1];
while (0 == ppEnum.Next(1, nodes, out actualResult))
{
Object obj;
nodes[0].GetProperty((uint)Microsoft.VisualStudio.VSConstants.VSITEMID_ROOT, (int)Microsoft.VisualStudio.Shell.Interop.__VSHPROPID.VSHPROPID_ExtObject, out obj);
Project project = obj as Project;
if (project != null)
{
if (string.IsNullOrEmpty(projectKind))
{
projects.Add(project);
}
else if (projectKind.Equals(project.Kind, StringComparison.InvariantCultureIgnoreCase))
{
projects.Add(project);
}
}
}
}
return projects.ToArray();
}
For IVsSolution interface, we can get it in this way:
IVsSolution vs = GetService(typeof(SVsSolution)) as IVsSolution;
Thanks, Avery - Marked As Answer byAvery_wu Wednesday, July 29, 2009 12:02 PM
-
| | Avery_wu Wednesday, July 29, 2009 12:01 PM |
|