如何获得的项目清单当前Visual Studio解决方案? [英] How to get list of projects in current Visual studio solution?

查看:449
本文介绍了如何获得的项目清单当前Visual Studio解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在任何开放的解决方案开包管理器控制台,它表明解决方案的所有项目。它是如何加载同样的解决方案的所有项目。
当我试着用下面所示代码,它被取了我,我已经打开了第一个解决方案的项目。

 私人清单<项目> GetProjects()
{
变种DTE =(DTE)Marshal.GetActiveObject(的String.Format(CultureInfo.InvariantCulture,VisualStudio.DTE {0} .0,targetVsVersion));
VAR项目= dte.Solution.OfType<项目>()了ToList()。
回报率的项目;
}


解决方案

下面是各种设置的功能,让您列举项目中给出的解决方案。这是你将如何与目前的解决方案使用它:

  //获取当前的解决方案
IVsSolution液=(IVsSolution )Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof运算(IVsSolution));
的foreach(在GetProjects工程项目(解决方案))
{
....
}

....

公共静态的IEnumerable< EnvDTE.Project> GetProjects(IVsSolution溶液)
{
的foreach
{
EnvDTE.Project项目= GetDTEProject(票数)(在GetProjectsInSolution(液)IVsHierarchy票数);
如果(项目!= NULL)
收益回报的项目;
}
}

公共静态的IEnumerable< IVsHierarchy> GetProjectsInSolution(IVsSolution溶液)
{
返回GetProjectsInSolution(溶液,__VSENUMPROJFLAGS.EPF_LOADEDINSOLUTION);
}

公共静态的IEnumerable< IVsHierarchy> GetProjectsInSolution(IVsSolution解决方案,__VSENUMPROJFLAGS标志)
{
如果(溶液== NULL)
产量突破;

IEnumHierarchies enumHierarchies;
的Guid GUID = Guid.Empty;
solution.GetProjectEnum((UINT)标志,裁判GUID,出enumHierarchies);
如果(enumHierarchies == NULL)
产量突破;

IVsHierarchy []层级=新IVsHierarchy [1];
UINT进账;
,而(enumHierarchies.Next(1,等级,出牵强)== VSConstants.S_OK和放大器;&安培;取== 1)
{
如果(hierarchy.Length大于0&安培; &安培;!层级[0] = NULL)
收益率的回报层次[0];
}
}

公共静态EnvDTE.Project GetDTEProject(IVsHierarchy层次)
{
如果(等级== NULL)
抛出新ArgumentNullException(等级);

obj对象;
hierarchy.GetProperty(VSConstants.VSITEMID_ROOT,(INT)__ VSHPROPID.VSHPROPID_ExtObject,出OBJ);
回报的obj为EnvDTE.Project;
}


When we open Package Manager Console in any open solution, it shows all the projects of that solution. How it is loading all the projects of the same solution. When I tried with below shown code it is fetching me projects of the first solution which I have opened.

    private List<Project> GetProjects()
    {
        var dte = (DTE)Marshal.GetActiveObject(string.Format(CultureInfo.InvariantCulture, "VisualStudio.DTE.{0}.0", targetVsVersion));
        var projects = dte.Solution.OfType<Project>().ToList();
        return projects;
    }

解决方案

Here are a various set of functions that allow you to enumerate projects in a given solution. This is how you would use it with the current solution:

// get current solution
IVsSolution solution = (IVsSolution)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(IVsSolution));
foreach(Project project in GetProjects(solution))
{
    ....
}

....

public static IEnumerable<EnvDTE.Project> GetProjects(IVsSolution solution)
{
    foreach (IVsHierarchy hier in GetProjectsInSolution(solution))
    {
        EnvDTE.Project project = GetDTEProject(hier);
        if (project != null)
            yield return project;
    }
}

public static IEnumerable<IVsHierarchy> GetProjectsInSolution(IVsSolution solution)
{
    return GetProjectsInSolution(solution, __VSENUMPROJFLAGS.EPF_LOADEDINSOLUTION);
}

public static IEnumerable<IVsHierarchy> GetProjectsInSolution(IVsSolution solution, __VSENUMPROJFLAGS flags)
{
    if (solution == null)
        yield break;

    IEnumHierarchies enumHierarchies;
    Guid guid = Guid.Empty;
    solution.GetProjectEnum((uint)flags, ref guid, out enumHierarchies);
    if (enumHierarchies == null)
        yield break;

    IVsHierarchy[] hierarchy = new IVsHierarchy[1];
    uint fetched;
    while (enumHierarchies.Next(1, hierarchy, out fetched) == VSConstants.S_OK && fetched == 1)
    {
        if (hierarchy.Length > 0 && hierarchy[0] != null)
            yield return hierarchy[0];
    }
}

public static EnvDTE.Project GetDTEProject(IVsHierarchy hierarchy)
{
    if (hierarchy == null)
        throw new ArgumentNullException("hierarchy");

    object obj;
    hierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ExtObject, out obj);
    return obj as EnvDTE.Project;
}

这篇关于如何获得的项目清单当前Visual Studio解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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