获取 VS 插件或 DXCore 插件的解决方案/项目文件列表

发布于 2024-08-05 19:23:57 字数 309 浏览 8 评论 0原文

我正在尝试为 Visual Studio 编写一个插件,除其他外,它需要跟踪 Visual Studio 解决方案中的每个文件。我知道我需要订阅哪些事件(当打开解决方案时,当在其中添加/删除/编辑文件时,对于项目也是如此),但我不明白如何实际获取文件列表来自其中任何一个。

我最近安装了 CodeRush 并一直在使用 DXCore 框架。我对它的插件方法非常满意,但我仍然没有看到一种明显的方法来获取解决方案中的文件列表。

总结一下:如何通过 Visual Studio SDK 或 DXCore 获得解决方案及其项目中可靠的文件列表?

I am trying to write a add-in for Visual Studio that, among other things, needs to keep track of every file in a Visual Studio solution. I know what events I need to subscribe to (when a Solution is opened, when a file is added/removed/edited in it, the same for projects, etc), but I don't understand how to actually get a list of files from any of it.

I recently installed CodeRush and have been playing with the DXCore framework. I'm very happy with it's approach at plugins, but I still don't see an obvious way to get a list of files in the solution.

So to sum it up: How, via the Visual Studio SDK or DXCore, do I get a reliable list of files in the solution and it's projects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸩远一方 2024-08-12 19:23:57

谢谢,里德;你链接的文章让我足够深入,可以在几分钟内完成概念验证。

因为我觉得它是相关的,所以这里是我收集 ProjectItems 的迭代和递归方法。我在 DXCore 中做到了这一点,但同样的想法也适用于原始 Visual Studio SDK(DXCore 只是 SDK 的一个更好看的包装器)。 “解决方案”、“项目”、“项目”和“项目项”对象就位于 EnvDTE 中。

设置项目

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

迭代项目以拉取 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));
    }
}

最后,我为获取活动解决方案中的所有 ProjectItems 所做的递归

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

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;
}
叹梦 2024-08-12 19:23:57

使用 Visual Studio SDK 中的 DTE 即可轻松实现这一切。

您可以使用 项目界面。

您可以使用 ProjectItem 接口。

有关详细信息,我建议阅读 控制项目和解决方案

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.

鼻尖触碰 2024-08-12 19:23:57

请参阅EnvDTE:获取所有项目(SolutionFolder PITA)< /a>

该解决方案可以出色地获取解决方案中的所有项目,即使项目位于子文件夹中也是如此。

请务必阅读代码下面的注释,因为它指出了一个重要的修复,请使用它来获取 DTE2 而不是原始代码,否则它将无法获取正确的 Visual Studio 实例:(

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

代码已更新为包括以上修复)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文