Retrieving folder content inside a Solution (EnvDTE)

Manya 20 Reputation points
2023-05-12T08:10:40.8966667+00:00

I am working on trying to retrieve all the content that may exist inside a folder inside a Solution, with the hierarchy being: Solution->Folder->Project->...

The problem I am facing is that the elements inside folder are returned as kind files instead of folders:

The folders inside a Visual Studio solution are represented as ProjectItem objects with the Kind property set to the string value PhysicalFolder. However, when you iterate over the child ProjectItem objects of a ProjectItems collection, the projects are returned as ProjectItem objects with the Kind property set to the string value PhysicalFile instead of Project. And due to this reason it isn't possible to parse the different files that exist inside the project.

Is there any way I can retrieve the projects inside the folder as type project instead of as files?

This is the code I am using to parse the elements:

private bool TraverseProject(Project project, EnvDTE.DTE visualStudioDTE) {

	foreach(Project proj in visualStudioDTE.Solution.Projects) {
         	//Structure- Project->Folder
            if(string.Compare(proj.Kind, typeFolder, true) ==0) {
            	//Traverse projectItems inside folder
             	TraverseProject(proj);
             }
             //Structure- Project->File (.cs)
             else {
				//Traverse inside file- codeModel code element
             	TraverseProject(proj);
            }
	}
}

In case a folder is present directly below a solution, EnvDTE recognises the folder's proj.Kind as a physicalFile instead of recognizes it as PhysicalFolder.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,545 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,524 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,476 Reputation points Microsoft Vendor
    2023-05-15T05:17:41.2233333+00:00

    @Manya, Welcome to Microsoft Q&A, you could find the folder firstly in the solution and get the subproject of the folder.

    Here is a code example you could refer to.

     static void Main(string[] args)
            {
                DTE dte = (DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.17.0"); // Replace "17.0" with your version of Visual Studio
                string solutionPath = dte.Solution.FileName;
                string folderPath = Path.Combine(Path.GetDirectoryName(solutionPath), "Test"); // Replace "Test" with your own folder name
    
                foreach (Project item in dte.Solution)
                {
                    if(item.Kind== ProjectKinds.vsProjectKindSolutionFolder)
                    {
                        var innerProjects = GetSolutionFolderProjects(item);
                        foreach (var innerProject in innerProjects)
                        {
                            Console.WriteLine(  innerProject.Name);
                        }
                    }
                       
                   
                }
            }
            private static  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)
                    {
                        projects.Add(subProject);
                        //Carried out work and added projects as appropriate
                    }
                }
    
                return projects;
            }
    
    
    

    Result:(Test folder has two projects)

    User's image

    Hope my solution could help you.

    Best Regards,

    Jack

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.  

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful