Ai đó có thể đăng macro Visual Studio đi qua tất cả các tệp nguồn C# trong dự án và thêm biểu ngữ tệp? Thêm tín dụng nếu nó hoạt động đối với bất kỳ loại tệp nguồn nào (.cs, .xaml, v.v.).Cần macro Visual Studio để thêm biểu ngữ vào tất cả các tệp C#
Trả lời
Ở đây bạn đi, tôi cung cấp một ví dụ cho cs và .vb nhưng không nên khó khăn cho bạn để điều chỉnh nó để loại tập tin khác của bạn cần: Edited đệ quy thêm tiêu đề để phụ -folders
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
Xin cảm ơn, tôi sẽ bầu cho người này thay vì người có liên kết tới blog, vì điều này cho thấy cách liệt kê các dự án. – DSO
Tính năng này hoạt động tốt như thế nào đối với các tệp có nhà thiết kế, như WinForms? –
Nên thêm nó vào bất kỳ tệp CS hoặc VB nào trong giải pháp. Mà sẽ bao gồm các tập tin thiết kế - tuy nhiên như với bất kỳ 'tạo ra' tập tin, tập tin có thể thay đổi và bình luận của bạn có thể không còn ở đó. –
Wow - câu trả lời tuyệt vời. Quá xấu OP dường như không làm phiền Google. –
Cảm ơn bạn đã liên kết. Onorio, tôi đã thử google nhưng sử dụng từ "biểu ngữ" thay vì "tiêu đề" ... mà trả về kết quả hoàn toàn khác nhau. Không biết tại sao, tôi luôn gọi họ là biểu ngữ. Tất nhiên bây giờ vì bài viết này, bằng cách sử dụng từ "banner" trả về bài viết này rất! – DSO
Đây là câu chuyện của nó. Không, tôi đã không sửa lỗi này, đó là một bài tập cho người đọc. Và, điều này được thực hiện trên đỉnh đầu của tôi. (Ngoại trừ File commenter ... Đó là một Macro thực sự mà tôi sử dụng).
function CommentAllFiles
option explicit
Dim ActiveProjectFullName
Dim dte80 As EnvDTE80.Solution2
ActiveProjectFullName = dte80.Projects.Item(0).FullName
If ActiveProjectFullName = "" Then
MsgBox("No project loaded!")
Exit Sub
End If
Err.Number = 0
doc.Open(ActiveProjectFullName, "Text", True)
If Err.Number <> 0 Then
MsgBox("Open " + ActiveProjectFullName + " failed: " & Hex(Err.Number))
Exit Sub
End If
ActiveDocument.Goto(1, 1, vsMovementOptions.vsMovementOptionsMove)
' Build search string
Dim SearchString
Dim vsFindOptionsValue As Integer
SearchString = "^SOURCE=.*" + dn + "$"
while ActiveDocument.Selection.FindText(SearchString, vsFindOptions.vsFindOptionsFromStart + vsFindOptions.vsFindOptionsRegularExpression)
Dim TheFile
TheFile = ActiveDocument.Selection.Text
TheFile = Mid(TheFile, 8)
doc.Open(TheFile)
wend
ActiveDocument.Close()
end function
Cố gắng và đúng sự thật "Flower Box" bộ cộng:
Function IsClassDef()
Dim ColNum
Dim LineNum
Dim sText
sText = ActiveDocument.Selection.ToString()
If sText = "" Then
'ActiveDocument.Selection.WordRight(dsExtend)
'sText = ActiveDocument.Selection
'sText = ucase(trim(sText))
End If
If (sText = "CLASS") Then
IsClassDef = True
Else
IsClassDef = False
End If
End Function
Sub AddCommentBlock()
'DESCRIPTION: Add Commecnt block to header, CPP files and Class Defs
AddCPPFileDesc()
End Sub
Sub AddCPPFileDesc()
'DESCRIPTION: Add File desc block to the top of a CPP file
Dim selection As EnvDTE.TextSelection
ActiveDocument.Selection.StartOfLine()
Dim editPoint As EnvDTE.EditPoint
selection = DTE.ActiveDocument.Selection()
editPoint = selection.TopPoint.CreateEditPoint()
Dim bOk, sExt, IsCpp, IsHdr, sHeader, IsCSharp
bOk = True
IsCpp = False
IsCSharp = False
If ActiveDocument.Selection.CurrentLine > 10 Then
If MsgBox("You are not at the top of the file. Are you sure you want to continue?", vbYesNo + vbDefaultButton2) = vbNo Then
bOk = False
End If
End If
If (bOk) Then
sExt = ucase(right(ActiveDocument.Name, 4))
IsCpp = sExt = ".CPP"
IsHdr = Right(sExt, 2) = ".H"
IsCSharp = sExt = ".CS"
If (IsCpp) Then
sHeader = left(ActiveDocument.Name, len(ActiveDocument.Name) - 3) + "h"
FileDescTopBlock(1)
editPoint.Insert("#include " + Chr(34) + "StdAfx.h" + Chr(34) + vbLf)
editPoint.Insert("#include " + Chr(34) + sHeader + Chr(34) + vbLf)
ElseIf (IsCSharp) Then
FileDescTopBlock(1)
Else
If IsHdr Then
'If IsCLassDef() Then
'AddClassDef()
'Else
AddHeaderFileDesc()
'End If
Else
FileDescTopBlock(1)
End If
End If
End If
End Sub
Sub AddHeaderFileDesc()
FileDescTopBlock(0)
Dim selection As EnvDTE.TextSelection
ActiveDocument.Selection.StartOfLine()
Dim editPoint As EnvDTE.EditPoint
selection = DTE.ActiveDocument.Selection()
editPoint = selection.TopPoint.CreateEditPoint()
editPoint.Insert("#pragma once" + vbLf)
End Sub
Sub FileDescTopBlock(ByVal HasRevHistory)
'DESCRIPTION: Add File desc block to the top of a CPP file
Dim selection As EnvDTE.TextSelection
ActiveDocument.Selection.StartOfLine()
ActiveDocument.Selection.EndOfLine()
Dim sComment
sComment = ActiveDocument.Selection.ToString()
If Left(sComment, 2) = "//" Then
ActiveDocument.Selection.Delete()
sComment = LTrim(Mid(sComment, 3))
Else
sComment = ""
End If
Dim sLineBreak
Dim sFileName
Dim sBlock
sLineBreak = "////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////"
sFileName = ActiveDocument.Name
ActiveDocument.Selection.StartOfDocument()
sBlock = sLineBreak & vbLf & _
"// File : " & sFileName & vbLf & _
"// Author : Larry Frieson" & vbLf & _
"// Desc : " & sComment & vbLf & _
"// Date : " & CStr(Now.Date()) & vbLf & _
"//" & vbLf & _
"// Copyright © 20" + Right(CStr(Now.Year.ToString()), 2) + " MLinks Technologies. All rights reserved" + vbLf
If (HasRevHistory > 0) Then
sBlock = sBlock & _
"//" & vbLf & _
"// Revision History: " & vbLf & _
"// " & CStr(Now) & " created." & vbLf & _
"// " & vbLf
End If
sBlock = sBlock + sLineBreak + vbLf
Dim editPoint As EnvDTE.EditPoint
selection = DTE.ActiveDocument.Selection()
editPoint = selection.TopPoint.CreateEditPoint()
editPoint.Insert(sBlock)
End Sub
Hope this helps, hoặc ít nhất là cung cấp cho bạn một số ý tưởng. Một lần nữa, tôi đã không kiểm tra/gỡ lỗi "file looper" nguồn, tôi hình bạn có thể xử lý đó.
Larry
- 1. Tất cả các tệp Visual Studio 2005 này là gì?
- 2. "Tabify" tất cả các tệp trong giải pháp Visual Studio?
- 3. Làm thế nào để định dạng tất cả các tệp trong Visual Studio 2012?
- 4. Thêm Thư viện vào Visual Studio 2008 C++ Project
- 5. Cách xuất tệp exe trong Visual Studio Với tất cả các tệp đã sử dụng
- 6. Thêm "# coding: utf-8" vào tất cả các tệp
- 7. Chạy tất cả các thử nghiệm mở trong Visual Studio
- 8. Cách cấu hình Visual Studio để kết hợp tất cả các tệp TypeScript vào một tệp JavaScript?
- 9. Visual Studio 2008 - Biểu tượng thanh trạng thái Biểu ngữ?
- 10. Visual Studio 2010 Macro vấn đề - Quit làm việc tất cả cùng nhau
- 11. Nhập biểu đồ UML vào Visual Studio
- 12. Force Visual Studio (2010) để lưu tất cả các tệp trong UTF-8
- 13. Visual Studio thêm tệp bin vào dự án
- 14. Visual Studio 2010 không xuất bản tất cả App_Data
- 15. CMake - Tự động thêm tất cả các tệp trong một thư mục vào một mục tiêu?
- 16. Macro của Visual Studio: Tìm tệp không được đưa vào dự án?
- 17. Làm cách nào để tôi có thể tạo Visual Studio Macro để đính kèm trình gỡ lỗi vào tất cả các phiên bản của w3wp.exe?
- 18. Macro trong Visual Studio 2010 tải tệp cài đặt
- 19. Thêm các mục vào trình đơn ngữ cảnh của Visual Studio
- 20. Visual Studio 2010: Thêm lệnh vào thanh công cụ
- 21. Làm cách nào để xuất tất cả mã nguồn từ Visual Studio sang tệp văn bản?
- 22. làm cách nào để mở TẤT CẢ các tệp excel từng cái một và chạy macro
- 23. Visual Studio Add-In - thêm một mục trình đơn ngữ cảnh vào giải pháp thám hiểm
- 24. Thêm các tệp hiện có vào dự án Visual Studio 2010 khác nhau
- 25. Chạy macro trong tất cả các bộ đệm trong vim
- 26. Cách thêm nút vào Visual Studio Intellisense
- 27. Visual Studio Web Triển khai không xuất bản tất cả các tệp
- 28. Lưu tất cả các tệp trước khi tạo bản dựng trong Visual Studio 2012?
- 29. Tại sao Visual Studio (2008) tạo tất cả các tệp * .vsmdi này
- 30. Visual Studio C++ 2010 Cách thêm ICON vào ứng dụng bảng điều khiển
VS 2013 có một add-in đẹp từ [Rubicon CNTT] (http://licensemanager.codeplex.com/) cho phép bạn thêm, xóa và cập nhật một tiêu đề cho tất cả các tập tin với loại tiện ích được khai báo trong tệp giấy phép của bạn. Cài đặt trực tiếp bằng cách vào Tools -> 'Extensions and Updates' -> 'Online' -> tìm kiếm 'headers' và tìm kết quả 'License Header Manager'. Bây giờ chỉ cần nhấp chuột phải vào thư mục dự án của bạn và sử dụng trình đơn 'tiêu đề giấy phép' mới. –