Get a list of Solution/Project Files for VS Add-in or DXCore Plugin

*爱你&永不变心* 提交于 2019-11-28 06:32:48

This is all available easily using DTE in the Visual Studio SDK.

You can get a list of projects in a solution using the Projects interface.

You can get a list of items in a project using the ProjectItem interface.

For more information, I'd recommend reading up on Controlling Projects and Solutions.

Thanks, Reed; the article you linked got me far enough to get a proof of concept churned out in a couple minutes.

Since I feel it's related, here is the iteration and recursive means by which I collected the ProjectItems. I did this in DXCore, but the same idea applies to the raw Visual Studio SDK (DXCore is merely a nicer looking wrapper over the SDK). The 'Solution', 'Projects', 'Project', and 'ProjectItem' objects are right there in EnvDTE.

Setting Projects

EnvDTE.Solution solution = CodeRush.ApplicationObject.Solution;
EnvDTE.Projects projects = solution.Projects;

Iterating over the Projects to pull ProjectItems

var projects = myProjects.GetEnumerator();
while (projects.MoveNext())
{
    var items = ((Project)projects.Current).ProjectItems.GetEnumerator();
    while (items.MoveNext())
    {
        var item = (ProjectItem)items.Current;
        //Recursion to get all ProjectItems
        projectItems.Add(GetFiles(item));
    }
}

Finally, The recursion I do for getting all ProjectItems in the active Solution

ProjectItem GetFiles(ProjectItem item)
{
    //base case
    if (item.ProjectItems == null)
        return item;

    var items = item.ProjectItems.GetEnumerator();
    while (items.MoveNext())
    {
        var currentItem = (ProjectItem)items.Current;
        projectItems.Add(GetFiles(currentItem));
    }

    return item;
}
Contango

See EnvDTE : Getting all projects (the SolutionFolder PITA)

This solution works brilliantly to get all of the projects in the solution, even if the projects are in subfolders.

Be sure to read the comments below the code, as it points out an essential fix, use this to grab DTE2 instead of the original code, or else it doesn't get the correct instance of Visual Studio:

DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2;

(the code was updated to include above fix)

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