Visual Studio 宏:查找项目中未包含的文件? [英] Visual Studio macro: Find files that aren't included in the project?
问题描述
我想编写一个宏来爬取我的项目目录中的文件并查找项目中未包含的文件.
I'd like to write a macro to crawl through the files in my project directory and find files that aren't included in the project.
在玩弄 DTE 对象时,我看到 Project
对象有 ProjectItems
;如果一个 ProjectItem
代表一个目录,那么它就有自己的 ProjectItems
集合.这给了我项目中包含的所有文件.
In playing around with the DTE object, I see that the Project
object has ProjectItems
; if a ProjectItem
represents a directory, then it has its own ProjectItems
collection. This gives me all files that are included in the project.
所以我可以递归地爬取每个 ProjectItems 集合,并且对于作为目录的每个 ProjectItem,检查文件系统中是否存在没有相应 ProjectItem 的文件.不过,这似乎很笨拙.
So I could crawl recursively through each ProjectItems collection, and for each ProjectItem that's a directory, check to see if there are files in the file system that don't have a corresponding ProjectItem. This seems clumsy, though.
有什么更简单的方法来解决这个问题吗?
Any ideas of a simpler way to approach this?
推荐答案
感谢 @JaredPar 和 @lpthnc 为我指明了正确的方向.我最终使用了一种与上面@JaredPar 概述的方法非常相似的方法.这是我的工作宏 FWIW.
Thanks to @JaredPar and @lpthnc for pointing me in the right direction. I ended up using an approach very similar to what @JaredPar outlines above. Here's my working macro 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
这篇关于Visual Studio 宏:查找项目中未包含的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!