感谢@JaredPar 和@lpthnc 为我指明了正确的方向。我最终使用了一种与上面@JaredPar 概述的方法非常相似的方法。这是我的工作宏 FWIW。
Imports System.IO
Imports System.Collections.Generic
Imports EnvDTE
Public Module Main
Sub IncludeNewFiles()
Dim Count As Integer = 0
For Each Project As Project In DTE.Solution.Projects
If Project.UniqueName.EndsWith(".vbproj") Then
Dim NewFiles As List(Of String) = GetFilesNotInProject(Project)
For Each File In NewFiles
Project.ProjectItems.AddFromFile(File)
Next
Count += NewFiles.Count
End If
Next
DTE.StatusBar.Text = String.Format("{0} new file{1} included in the project.", Count, If(Count = 1, "", "s"))
End Sub
Private Function GetAllProjectFiles(ByVal ProjectItems As ProjectItems, ByVal Extension As String) As List(Of String)
GetAllProjectFiles = New List(Of String)
For Each ProjectItem As ProjectItem In ProjectItems
For i As Integer = 1 To ProjectItem.FileCount
Dim FileName As String = ProjectItem.FileNames(i)
If Path.GetExtension(fileName).ToLower = Extension Then
GetAllProjectFiles.Add(fileName)
End If
Next
GetAllProjectFiles.AddRange(GetAllProjectFiles(ProjectItem.ProjectItems, Extension))
Next
End Function
Private Function GetFilesNotInProject(ByVal Project As Project) As List(Of String)
Dim StartPath As String = Path.GetDirectoryName(Project.FullName)
Dim ProjectFiles As List(Of String) = GetAllProjectFiles(Project.ProjectItems, ".vb")
GetFilesNotInProject = New List(Of String)
For Each file In Directory.GetFiles(StartPath, "*.vb", SearchOption.AllDirectories)
If Not ProjectFiles.Contains(file) Then GetFilesNotInProject.Add(file)
Next
End Function
End Module