Dus*_*sda 13 c# plugins add-in dxcore visual-studio-sdk
我正在尝试为Visual Studio编写一个加载项,除其他外,需要跟踪Visual Studio解决方案中的每个文件.我知道我需要订阅什么事件(当打开解决方案时,在其中添加/删除/编辑文件时,项目中的文件相同等),但我不明白如何实际获取文件列表从任何一个.
我最近安装了CodeRush并且一直在使用DXCore框架.我对插件的方法感到非常满意,但我仍然没有看到一个明显的方法来获取解决方案中的文件列表.
总结一下:如何通过Visual Studio SDK 或 DXCore获得解决方案及其项目中可靠的文件列表?
Dus*_*sda 18
谢谢,里德; 你链接的文章让我足够远,可以在几分钟内得到一个概念证明.
由于我觉得它是相关的,这里是我收集ProjectItems的迭代和递归方法.我在DXCore中做到了这一点,但同样的想法适用于原始的Visual Studio SDK(DXCore只是一个比SDK更好看的包装器).EnvDTE中的"解决方案","项目","项目"和"ProjectItem"对象就在那里.
设置项目
EnvDTE.Solution solution = CodeRush.ApplicationObject.Solution;
EnvDTE.Projects projects = solution.Projects;
Run Code Online (Sandbox Code Playgroud)
迭代项目以拉取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));
}
}
Run Code Online (Sandbox Code Playgroud)
最后,我做的递归是为了在活动的解决方案中获取所有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;
}
Run Code Online (Sandbox Code Playgroud)
在Visual Studio SDK中使用DTE可以轻松实现这一切.
您可以使用ProjectItem接口获取项目中的项目列表.
有关更多信息,我建议您阅读控制项目和解决方案.