感謝@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
涉足的所有目錄的列表中,找到他們每個文件,其完整路徑保存到一組數據結構。現在瀏覽項目中的所有文件,並從集合中刪除它們(預先設置正確的路徑)。然後檢查你有什麼...... – 2010-01-04 15:30:51
謝謝 - 爲什麼不只是發佈這個答案? – 2010-01-04 15:59:16