如何在 VSIX 项目的解决方案文件夹中获取项目 [英] How to get project inside of Solution Folder in VSIX project

查看:53
本文介绍了如何在 VSIX 项目的解决方案文件夹中获取项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,在 Visual Studio 扩展中执行自定义构建任务.我需要确定我的自定义项目类型的项目.如果它们位于解决方案的根目录上,我可以很好地做到这一点,但是当它位于解决方案文件夹中时就会出现问题.我可以将解决方案文件夹作为 EnvDTE.Project 获取,但不确定如何从该文件夹中获取项目.

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.

我以为我可以从项目 Collection 属性中获取它,但结果为空.

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
           }

推荐答案

我通过更多的研究和反复试验设法解决了这个问题.万一其他人提出这个问题,我将主要代码更改为

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.
             }
         }
    }
}

GetSolutionFolderForProjects 的代码是

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.

这篇关于如何在 VSIX 项目的解决方案文件夹中获取项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆