获取Visual Studio解决方案文件和文件夹 [英] Get Visual Studio Solution Files and Folders

查看:263
本文介绍了获取Visual Studio解决方案文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发CustomControl,我需要枚举Solution中的文件和文件夹.反正有这样做吗?

- 该方案是: 实际上,我正在尝试创建一个简单的绑定控件,该控件仅枚举从指定类继承的类,然后用户可以选择类并将其绑定到相关Form的控件,要做到这一点,我找不到在设计模式下有用的任何东西,这就是我要枚举解决方案文件和文件夹的原因.

我认为最好的方法是通过Visual Studio Extension API中的EnvDTE类.有一个很好的内置示例:打开宏资源管理器"并导航到实用工具/ListProj".然后右键单击并选择编辑"以显示在Visual Basic中实现它的代码.这应该为您提供足够的信息,以便能够在C#中复制逻辑(类和方法的名称相同)

它的想法很简单-您只需递归枚举ProjectItems()成员.每个节点本身可能还具有ProjectItems()个成员

Dim project As Project
project = DTE.ActiveSolutionProjects(0)
ListProjAux(project.ProjectItems(), 0)

Sub ListProjAux(ByVal projectItems As EnvDTE.ProjectItems, ByVal level As Integer)
  Dim projectItem As EnvDTE.ProjectItem
  For Each projectItem In projectItems
    projectItems2 = projectItem.ProjectItems
       ListProjAux(projectItems2, level + 1)
  Next
End Sub

请注意,在此示例中,我省略了一些null检查,但它们在ListProj示例中.

I'm trying to develop a CustomControl and I need to enumerate Files and Folder inside Solution. Is there anyway to do this?

--Edit: The scenario is: Actually I'm trying to create a simple binding control that enumerate only classes which is inherited from a specified class then user can select class and bind it to related Form's controls, to do this I couldn't find anything useful in design-mode, that's what I ask to enumerate solution files and folders.

解决方案

I think the best way to do this is via the EnvDTE class in the Visual Studio Extension API. There's a good example built-in: open the Macro Explorer and navigate to Utilities/ListProj. Then right-click and choose 'Edit' to show the code that implements it in Visual Basic. This should give you enough information to be able to replicate the logic in C# (the class and method names are the same)

The idea of it is simple - you just enumerate the ProjectItems() members recursively. Each node may itself have further ProjectItems() members

Dim project As Project
project = DTE.ActiveSolutionProjects(0)
ListProjAux(project.ProjectItems(), 0)

Sub ListProjAux(ByVal projectItems As EnvDTE.ProjectItems, ByVal level As Integer)
  Dim projectItem As EnvDTE.ProjectItem
  For Each projectItem In projectItems
    projectItems2 = projectItem.ProjectItems
       ListProjAux(projectItems2, level + 1)
  Next
End Sub

Note that I've omitted some of the null checks in this example, but they're in the ListProj sample.

这篇关于获取Visual Studio解决方案文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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