Search code examples
c#visual-studio-extensionsvsix

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
           }

Solution

  • 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.