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
}
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With