我最终通过拼接两种不同的方法(一种用于查找文件夹,另一种用于遍历文件)来实现自己的工作。
找到我想要的文件夹的一个在这里,切换触发宏:
http://msdn.microsoft.com/en-us/library/bb264485(v=vs.80).aspx
迭代宏是这里有用的答案:
需要 Visual Studio 宏才能将横幅添加到所有 C# 文件
最终的解决方案:
Imports System
Imports System.ComponentModel
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module iterateFolder
Sub IterateFiles(Optional ByVal dbProjName As String = "")
Dim project As Project
Dim parentFolder As String = ""
Dim courseFolder As String = ""
If (String.IsNullOrEmpty(dbProjName)) Then
dbProjName = InputBox("Type the database project name.")
If (String.IsNullOrEmpty(dbProjName)) Then
Return
End If
End If
If (String.IsNullOrEmpty(parentFolder)) Then
parentFolder = InputBox("Type the parent folder name.")
If (String.IsNullOrEmpty(parentFolder)) Then
Return
End If
End If
If (String.IsNullOrEmpty(courseFolder)) Then
courseFolder = InputBox("Type the child folder name.")
If (String.IsNullOrEmpty(courseFolder)) Then
Return
End If
End If
For Each project In DTE.Solution
Dim projectItem As EnvDTE.ProjectItem
If (dbProjName.Equals(project.Name)) Then
For Each projectItem In project.ProjectItems()
If (projectItem.Name = parentFolder) Then
Dim subItem As EnvDTE.ProjectItem
For Each subItem In projectItem.ProjectItems()
If (subItem.Name = courseFolder) Then
IterateProjectFiles(subItem.ProjectItems)
End If
Next
End If
Next
End If
Next
End Sub
Private Sub IterateProjectFiles(ByVal prjItms As ProjectItems)
For Each file As ProjectItem In prjItms
If file.SubProject IsNot Nothing Then
DTE.ExecuteCommand("View.SolutionExplorer")
If file.Name.EndsWith(".aspx") Then
file.Open()
file.Document.Activate()
DTE.ExecuteCommand("Macros.Abba.Module1.AbbaZabba")
End If
IterateProjectFiles(file.ProjectItems)
ElseIf file.ProjectItems IsNot Nothing AndAlso file.ProjectItems.Count > 0 Then
DTE.ExecuteCommand("View.SolutionExplorer")
If file.Name.EndsWith(".aspx") Then
file.Open()
file.Document.Activate()
DTE.ExecuteCommand("Macros.Abba.Module1.AbbaZabba")
End If
IterateProjectFiles(file.ProjectItems)
Else
DTE.ExecuteCommand("View.SolutionExplorer")
If file.Name.EndsWith(".aspx") Then
file.Open()
file.Document.Activate()
DTE.ExecuteCommand("Macros.Abba.Module1.AbbaZabba")
End If
End If
Next
End Sub
End Module
您可以硬编码项目和文件夹名称,或者只是其中的一些名称,或者将它们全部留空,具体取决于您需要多少多功能性。
我应该在这里注意 abbazabba 宏执行文件的关闭和保存,但如果这对任何人有用,您可以从本文的第二个链接中获得如何做到这一点。