c# – 如何在VSIX项目中的Solution Folder中获取项目

前端之家收集整理的这篇文章主要介绍了c# – 如何在VSIX项目中的Solution Folder中获取项目前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
您好我有一个问题,在Visual Studio扩展中有自定义生成任务.我需要识别我的自定义项目类型的项目.如果它们位于解决方案的根目录下,我可以做到这一点,但是当它在解决方文件夹中时会出现问题.我可以将解决方文件夹作为EnvDTE.Project获取,但我不确定如何从该文件夹中获取项目.

我以为我可以从项目Collection属性获取它,但这是null.

任何帮助将不胜感激.

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
           }

解决方法

我设法通过更多的研究和一些反复试验来解决这个问题.如果有其他人提出这个问题,我将主要代码更改为
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的代码

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

希望这有助于其他人.

猜你在找的C#相关文章