How to get project inside of Solution Folder in VSIX project

橙三吉。 提交于 2021-02-07 05:37:27

问题


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
           }

回答1:


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.




回答2:


I had a similar question within a T4 template, where I had to find a project by name within the solution, at any level: root, folder, nested folder.

For reference purposes, I'm pasting it here. It's totally based on the solution from @DaveGreen, so credits to him:

<#@ import namespace="System.Linq" #>
<#
var dte = (DTE)hostServiceProvider.GetService(typeof(DTE));

var project = GetProject(dte.Solution, "ProjectName");
#>
<#+
public static Project GetProject(Solution solution, string name)
{
    var project = GetProject(solution.Projects.OfType<Project>(), name);

    if (project == null)
    {
        throw new Exception($"Project {name} not found in solution");
    }

    return project;
}

public static Project GetProject(IEnumerable<Project> projects, string name)
{
    foreach (Project project in projects)
    {
        var projectName = project.Name;
        if (projectName == name)
        {
            return project;
        }
        else if (project.Kind == EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder)
        {
            var subProjects = project
                .ProjectItems
                .OfType<ProjectItem>()
                .Where(item => item.SubProject != null)
                .Select(item => item.SubProject);

            var projectInFolder = GetProject(subProjects, name);

            if (projectInFolder != null)
            {
                return projectInFolder;
            }
        }
    }

    return null;
}
#>


来源:https://stackoverflow.com/questions/38740773/how-to-get-project-inside-of-solution-folder-in-vsix-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!