gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:05:22 24 4
gpt4 key购买 nike

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

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

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

最佳答案

谢谢,里德;你链接的文章让我在几分钟内得到了一个概念证明。

因为我觉得它是相关的,所以这是我收集 ProjectItems 的迭代和递归方法。我是在 DXCore 中这样做的,但同样的想法也适用于原始 Visual Studio SDK(DXCore 只是 SDK 的一个更好看的包装器)。 “Solution”、“Projects”、“Project”和“ProjectItem”对象就在 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));
}
}

最后,我为获取事件解决方案中的所有项目项所做的递归

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

关于c# - 获取 VS 插件或 DXCore 插件的解决方案/项目文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1434720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com