该程序可以统计项目中一共有多少个文件,一共有多少行程序,随便写的,有bug请指出。谢谢
如果项目中文件很多,统计速度会很慢,如果需要中断,可以随时双击任务栏右边的宏运行的图标
Option Strict Off
Option Explicit Off
Imports EnvDTE
Imports System.Diagnostics
Imports System.Data
Public Module RecordingModule
Private dt As New DataTable
Sub GetProject(ByVal items As ProjectItems)
For Each item As EnvDTE.ProjectItem In items
If item.ProjectItems.Count > 0 Then
GetProject(item.ProjectItems)
End If
Count(item)
Next
End Sub
Sub Count(ByVal item As EnvDTE.ProjectItem)
Dim fileName As String = item.Name.ToLower()
Dim extName As String
If fileName.IndexOf(".") < 0 Then
extName = "Folder"
Else
extName = fileName.Substring(fileName.LastIndexOf("."))
End If
Dim ArrayDr As DataRow() = dt.Select("ExtName='" + extName + "'")
Dim dr As DataRow
If ArrayDr.Length = 0 Then
dr = dt.NewRow()
dr("ExtName") = extName
dt.Rows.Add(dr)
Else
dr = ArrayDr(0)
End If
If Not (dr("File") Is System.DBNull.Value) Then
dr("File") = CType(dr("File"), System.Int32) + 1
Else
dr("File") = 1
End If
Try
Dim objWindow As Window = item.Open(Constants.vsViewKindCode)
Dim objDocument As Document = item.Document
Dim objTextDocument As TextDocument = objDocument.Object("TextDocument")
Dim strDocumentText As String = objTextDocument.StartPoint.CreateEditPoint().GetText(objTextDocument.EndPoint)
If Not (dr("Line") Is System.DBNull.Value) Then
dr("Line") = CType(dr("Line"), System.Int32) + strDocumentText.Split(vbCrLf).Length()
Else
dr("Line") = strDocumentText.Split(vbCrLf).Length()
End If
DTE.ExecuteCommand("Window.CloseDocumentWindow")
DTE.ActiveWindow.Close(vsSaveChanges.vsSaveChangesNo)
Catch
End Try
End Sub
Sub TemporaryMacro()
dt = New DataTable
If Not dt.Columns.Contains("ExtName") Then
dt.Columns.Add("ExtName", System.Type.GetType("System.String")) '扩展名
dt.Columns.Add("File", System.Type.GetType("System.Int32")) '行数
dt.Columns.Add("Line", System.Type.GetType("System.Int32")) '行数
dt.Columns.Add("Cha", System.Type.GetType("System.Int32")) '字符数量
End If
Dim proj As Project = DTE.ActiveSolutionProjects(0)
GetProject(proj.ProjectItems())
Dim outPutStr As String = "FileType".PadRight(10) & "File".PadLeft(10) & "Line".PadLeft(10) & " " & vbCrLf
For Each dr As DataRow In dt.Rows
outPutStr += dr("ExtName").ToString().PadRight(10) & dr("File").ToString().PadLeft(10) & dr("Line").ToString().PadLeft(10) & vbCrLf
Next
MsgBox(outPutStr)
End Sub
End Module
Option Explicit Off
Imports EnvDTE
Imports System.Diagnostics
Imports System.Data
Public Module RecordingModule
Private dt As New DataTable
Sub GetProject(ByVal items As ProjectItems)
For Each item As EnvDTE.ProjectItem In items
If item.ProjectItems.Count > 0 Then
GetProject(item.ProjectItems)
End If
Count(item)
Next
End Sub
Sub Count(ByVal item As EnvDTE.ProjectItem)
Dim fileName As String = item.Name.ToLower()
Dim extName As String
If fileName.IndexOf(".") < 0 Then
extName = "Folder"
Else
extName = fileName.Substring(fileName.LastIndexOf("."))
End If
Dim ArrayDr As DataRow() = dt.Select("ExtName='" + extName + "'")
Dim dr As DataRow
If ArrayDr.Length = 0 Then
dr = dt.NewRow()
dr("ExtName") = extName
dt.Rows.Add(dr)
Else
dr = ArrayDr(0)
End If
If Not (dr("File") Is System.DBNull.Value) Then
dr("File") = CType(dr("File"), System.Int32) + 1
Else
dr("File") = 1
End If
Try
Dim objWindow As Window = item.Open(Constants.vsViewKindCode)
Dim objDocument As Document = item.Document
Dim objTextDocument As TextDocument = objDocument.Object("TextDocument")
Dim strDocumentText As String = objTextDocument.StartPoint.CreateEditPoint().GetText(objTextDocument.EndPoint)
If Not (dr("Line") Is System.DBNull.Value) Then
dr("Line") = CType(dr("Line"), System.Int32) + strDocumentText.Split(vbCrLf).Length()
Else
dr("Line") = strDocumentText.Split(vbCrLf).Length()
End If
DTE.ExecuteCommand("Window.CloseDocumentWindow")
DTE.ActiveWindow.Close(vsSaveChanges.vsSaveChangesNo)
Catch
End Try
End Sub
Sub TemporaryMacro()
dt = New DataTable
If Not dt.Columns.Contains("ExtName") Then
dt.Columns.Add("ExtName", System.Type.GetType("System.String")) '扩展名
dt.Columns.Add("File", System.Type.GetType("System.Int32")) '行数
dt.Columns.Add("Line", System.Type.GetType("System.Int32")) '行数
dt.Columns.Add("Cha", System.Type.GetType("System.Int32")) '字符数量
End If
Dim proj As Project = DTE.ActiveSolutionProjects(0)
GetProject(proj.ProjectItems())
Dim outPutStr As String = "FileType".PadRight(10) & "File".PadLeft(10) & "Line".PadLeft(10) & " " & vbCrLf
For Each dr As DataRow In dt.Rows
outPutStr += dr("ExtName").ToString().PadRight(10) & dr("File").ToString().PadLeft(10) & dr("Line").ToString().PadLeft(10) & vbCrLf
Next
MsgBox(outPutStr)
End Sub
End Module