gpt4 book ai didi

visual-studio - 需要Visual Studio宏才能将横幅广告添加到所有C#文件

转载 作者:行者123 更新时间:2023-12-04 03:02:57 25 4
gpt4 key购买 nike

有人可以发布一个Visual Studio宏,该宏遍历项目中的所有C#源文件并添加文件标题吗?如果它适用于任何类型的源文件(.cs,.xaml等),则可享有额外的信誉。

最佳答案

在这里,我为.cs和.vb提供了一个示例,但不难让您根据其他文件类型的需要对其进行调整:编辑后可以将 header 递归添加到子文件夹

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 - 需要Visual Studio宏才能将横幅广告添加到所有C#文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/415101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com