EnvDTE iterate through subfolders within solution

1.4k views Asked by At

Have a below structure within VS Solution:

Now, I want to get reference to "Catalogs" folder and store it in variable:

        //Solution_Name
        //    ConfigurationObjects
        //        Catalogs
        //        Documents

    foreach (Project item1 in AttachedSolution.Projects)
    {
        if (item1.Kind == ProjectKinds.vsProjectKindSolutionFolder)
        {
            if (item1.Name == "ConfigurationObjects")
            {
                SolutionFolder catalogBaseFolder = (SolutionFolder)item1.Object;

                foreach (ProjectItem item2 in item1.ProjectItems)
                {
                    if ( item2.Name == "Catalogs")
                    {
                        this.CatalogObjectsFolder = (SolutionFolder)item2;
                    // raises error here: Unable to cast COM object of type                   
                    //'System.__ComObject' 
                    //to interface type 'EnvDTE80.SolutionFolder'.
                    };
                };
            };
        };
    };

Thanks for help.

1

There are 1 answers

0
Carlos Quintero On BEST ANSWER

Solution folders that are not first level are modeled as EnvDTE.Project, not as EnvDTE80.SolutionFolder. You can get the project from the projectItem using the projectItem.SubProject property. And then use Project.Object to get the EnvDTE80.SolutionFolder

See:

HOWTO: Navigate the files of a solution from a Visual Studio .NET macro or add-in

How to create a solution folder inside another solution folder