Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get project inside of Solution Folder in VSIX project

Hi I am having a problem, with a custom build task inside of a Visual Studio Extension. I need to identify projects of my custom project type. I can do this fine if they are on the root of the solution, but the problem occurs when it is inside of a solution folder. I can get the solution folder as a EnvDTE.Project, but am not sure how to get projects from within that folder.

I thought I would be able to get it from the projects Collection property but that is null.

Any assistance 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
           }
like image 727
DaveGreen Avatar asked Aug 03 '16 10:08

DaveGreen


People also ask

Can I add project to solution Visual Studio?

To add an existing project to a solutionIn Solution Explorer, select the solution. On the File menu, point to Add, and click Existing Project. In the Add Existing Project dialog box, locate the project you want to add, select the project file, and then click Open. The project is added to the selected solution.

What is a solution folder in Visual Studio?

A solution folder is a container for one or more related projects. You can create many folders within a Visual Studio solution, including nested folders. They appear in the Solution Explorer as expandable and collapsible sections.


1 Answers

I managed to resolve this with a bit more research and some trial and error. In case anybody 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 the 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 somebody else.

like image 81
DaveGreen Avatar answered Sep 21 '22 11:09

DaveGreen