How to get a project inside Solution Folders in a VSIX project

Hi I am having a problem setting a custom assembly inside Visual Studio Extension. I need to define projects of my project type. I can do it well if they are at the root of the solution, but the problem occurs when it is inside the solutions folder. I can get the solution folder as EnvDTE.Project, but I'm not sure how to get the projects from this folder.

I thought I could get it from the Collection Collection property, but this is null.

Any help would be greatly appreciated.

if (Scope == EnvDTE.vsBuildScope.vsBuildScopeSolution) { DTE2 dte2 = Package.GetGlobalService(typeof(EnvDTE.DTE)) as DTE2; var sol = dte2.Solution; EnvDTE.DTE t = dte2.DTE; var x = t.Solution.Projects; foreach(var proj in x) { try { var project = proj as EnvDTE.Project; var guid = GetProjectTypeGuids(project); if (guid.Contains("FOLDERGUID")) { //here is where I would get the project from the folder } 
+5
source share
2 answers

I managed to resolve this with a bit more research and some trial and error. In case anyone else comes up with this problem, I changed the main code to

 if (Scope == EnvDTE.vsBuildScope.vsBuildScopeSolution) { errorListProvider.Tasks.Clear(); DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2; var sol = dte2.Solution; var projs = sol.Projects; foreach(var proj in sol) { var project = proj as Project; if (project.Kind == ProjectKinds.vsProjectKindSolutionFolder) { var innerProjects = GetSolutionFolderProjects(project); foreach(var innerProject in innerProjects) { //carry out actions here. } } } } 

The code for GetSolutionFolderForProjects was

 private IEnumerable<Project> GetSolutionFolderProjects(Project project) { List<Project> projects = new List<Project>(); var y = (project.ProjectItems as ProjectItems).Count; for(var i = 1; i <= y; i++) { var x = project.ProjectItems.Item(i).SubProject; var subProject = x as Project; if (subProject != null) { //Carried out work and added projects as appropriate } } return projects; } 

Hope this helps someone else.

+10
source

I had a similar question in the T4 template, where I had to search for a project by name inside the solution at any level: root, folder, inested folder.

For reference purposes, I am inserting it here. This is entirely based on a solution from @DaveGreen, so credit him:

 <#@ import namespace="System.Linq" #> <# var dte = (DTE)hostServiceProvider.GetService(typeof(DTE)); var project = GetProject(dte.Solution, "ProjectName"); #> <#+ public static Project GetProject(Solution solution, string name) { var project = GetProject(solution.Projects.OfType<Project>(), name); if (project == null) { throw new Exception($"Project {name} not found in solution"); } return project; } public static Project GetProject(IEnumerable<Project> projects, string name) { foreach (Project project in projects) { var projectName = project.Name; if (projectName == name) { return project; } else if (project.Kind == EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder) { var subProjects = project .ProjectItems .OfType<ProjectItem>() .Where(item => item.SubProject != null) .Select(item => item.SubProject); var projectInFolder = GetProject(subProjects, name); if (projectInFolder != null) { return projectInFolder; } } } return null; } #> 
0
source

All Articles