Including external folders in the solutions after being created (EnvDTE)

Manya 20 Reputation points
2023-06-29T15:49:12.2466667+00:00

I am trying to create a folder inside a solution using EnvDTE. I am able to create the folder in the solution folder using the API but I am unable to include this in the solution itself. I have tried to implement this using different functionalities that the API offers but it does not seem to work with any of them.

The functionality that made the most sense to me logically that should work, but did not is the following:

string folderPath = @"C:\MySolution\MyFolder"; 
EnvDTE.Project project = solution.Projects.Item(1); 
EnvDTE.ProjectItems projectItems = project.ProjectItems;

//Creates the physical folder 
EnvDTE.ProjectItem newFolder = projectItems.AddFolder(folderPath); 
// Add the new folder as a project item 
project.ProjectItems.AddFromFile(newFolder.FileNames[1]);

The exit error message it showed was the following:

System.Exception: 'Unable to add project item

or using:

solution.AddFromFile(folderPath);

Which gave an output error:

System.Runtime.InteropServices.COMException: 'The template specified cannot be found. Please check that the full path is correct.'

Is there any other way I can include this created folder in the solution?

Developer technologies | .NET | Other
Developer technologies | Visual Studio | Other
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Manya 20 Reputation points
    2023-07-07T14:01:04.3066667+00:00

    I was able to resolve these issues in the following manners:

    • Creation of folder directly inside a Solution-
    public static void AddingNewFolderInSolution(EnvDTE.DTE dte) {         
    	Solution2 sol = (Solution2)dte.Solution;         
    	Project solFolder = sol.AddSolutionFolder("NewFolderAdded");         
    	sol.SaveAs(sol.FileName);     
    }
    
    
    • Creation of folder directly inside a Project-
    public static void AddingNewFolderInProject(EnvDTE.DTE dte) {               
    	Solution sol = dte.Solution;         
    	Project proj = sol.Projects.Item(1);                             			
    	proj.ProjectItems.AddFolder(proj.FileName, Constants.vsProjectItemKindPhysicalFolder);            
    	proj.Save();     
    } 
    

    The addition into the project is done using proj.projectItems.AddFolder()

    • Creation of folder inside a Folder-
    public static void AddingNewFolderInProject(EnvDTE.DTE dte) {         
    	string pathToFolderOrProject = @"C:\Route\To\Folder";         
    	Solution sol = dte.Solution;         
    	//Accessing the first project as an example-
    	Project proj = sol.Projects.Item(1);         
    	foreach (ProjectItem projItem in proj.ProjectItems) {             
    		if (string.Compare(projItem.Kind, Constants.vsProjectItemKindPhysicalFolder) == 0) {                 			
    			projItem.ProjectItems.AddFolder(pathToFolderOrProject, Constants.vsProjectItemKindPhysicalFolder);             		
    		}         
    	}         
    	
    	proj.Save();     
    } 
    

    The addition of a folder inside an existing folder is done using projItem.projectItems.AddFolder() where the path is the path to the parent folder+Name of new folder to be created.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.