有人可以发布Visual Studio宏,它遍历项目中的所有C#源文件并添加文件横幅吗?如果适用于任何类型的源文件(.cs,.xaml等),则额外信用.
在这里,我提供.cs和.vb的示例,但是您应该很难将其调整为其他文件类型需求: 编辑以递归方式将标头添加到子文件夹
Sub IterateFiles() Dim solution As Solution = DTE.Solution For Each prj As Project In solution.Projects IterateProjectFiles(prj.ProjectItems) Next End Sub Private Sub IterateProjectFiles(ByVal prjItms As ProjectItems) For Each file As ProjectItem In prjItms If file.SubProject IsNot Nothing Then AddHeaderToItem(file) IterateProjectFiles(file.ProjectItems) ElseIf file.ProjectItems IsNot Nothing AndAlso file.ProjectItems.Count > 0 Then AddHeaderToItem(file) IterateProjectFiles(file.ProjectItems) Else AddHeaderToItem(file) End If Next End Sub Private Sub AddHeaderToItem(ByVal file As ProjectItem) DTE.ExecuteCommand("View.SolutionExplorer") If file.Name.EndsWith(".cs") OrElse file.Name.EndsWith(".vb") Then file.Open() file.Document.Activate() AddHeader() file.Document.Save() file.Document.Close() End If End Sub Private Sub AddHeader() Dim cmtHeader As String = "{0} First Line" Dim cmtCopyright As String = "{0} Copyright 2008" Dim cmtFooter As String = "{0} Footer Line" Dim cmt As String Select Case DTE.ActiveDocument.Language Case "CSharp" cmt = "//" Case "Basic" cmt = "'" End Select DTE.UndoContext.Open("Header Comment") Dim ts As TextSelection = CType(DTE.ActiveDocument.Selection, TextSelection) ts.StartOfDocument() ts.Text = String.Format(cmtHeader, cmt) ts.NewLine() ts.Text = String.Format(cmtCopyright, cmt) ts.NewLine() ts.Text = String.Format(cmtFooter, cmt) ts.NewLine() DTE.UndoContext.Close() End Sub
Visual Studio宏添加文件头