'copy frum example
Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As Boolean = True) As OutputWindowPane
Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
If show Then win.Visible = True
Dim ow As OutputWindow = win.Object
Dim owpane As OutputWindowPane
Try
owpane = ow.OutputWindowPanes.Item(Name)
Catch e As System.Exception
owpane = ow.OutputWindowPanes.Add(Name)
End Try
owpane.Activate()
Return owpane
End Function
Sub 清除内存()
Dim i As Int32
For i = 0 To 10
GC.Collect(10)
Next
End Sub
Public Sub 去除所有Aspx垃圾()
Try
DeleteAspxTagsInternal()
Catch x As Exception
MsgBox(x.ToString())
Throw
End Try
End Sub
Private Sub DeleteAspxTagsInternal()
Dim Output As OutputWindowPane = GetOutputWindowPane("我的工具")
Output.Clear()
Dim i As Int32
For i = 1 To DTE.Solution.Projects.Count
Dim proj As Project = DTE.Solution.Projects.Item(i)
If proj.Properties.Item("ProjectType").Value = 1 Then
Dim filename As String = projitem.Properties.Item("FullPath").Value
Dim count As Int32
count = ReplaceFile(filename)
Output.OutputString(filename & " OK - " & count)
Output.OutputString(vbCrLf)
Next
Output.OutputString(al.Count & " Files OK")
Output.OutputString(vbCrLf)
End If
Next
End Sub
Dim regexstrs As String() = New String() {"<FONT[^>]*>", "", "</FONT>", "", "<!DOCTYPE[^>]*>", "", "<META[^>]*>", "", "(\r\n)[ \t]*\r\n", "$1"}
'替换文件内容
Function ReplaceFile(ByVal filename As String) As Int32
Dim regcount As Int32 = regexstrs.Length / 2
Dim regexs As Regex() = New Regex(regcount) {}
Dim i As Int32
For i = 0 To regcount - 1
regexs(i) = New Regex(regexstrs(i * 2), RegexOptions.ECMAScript Or RegexOptions.IgnoreCase Or RegexOptions.Compiled)
Next
Dim s As String
Dim sr As New StreamReader(filename, Encoding.GetEncoding("gb2312"))
s = sr.ReadToEnd()
sr.Close()
Dim count As Int32 = 0
For i = 0 To regcount - 1
count = count + regexs(i).Matches(s).Count()
Next
If count = 0 Then
Return 0
End If
Dim fs As New FileStream(filename, FileMode.Truncate, FileAccess.Write)
For i = 0 To regcount - 1
While regexs(i).IsMatch(s)
s = regexs(i).Replace(s, regexstrs(i * 2 + 1))
End While
Next
Dim sw As New StreamWriter(fs, Encoding.GetEncoding("gb2312"))
sw.Write(s)
sw.Flush()
fs.Close()
Return count
End Function
'取得包含文件ProjectItem的ArrayList
Function GetFileItems(ByVal proj As Project, ByVal ext As String) As ArrayList
Dim al As New ArrayList
AppendToArrayList(al, proj.ProjectItems, ext)
Return al
End Function
'递归把符合ext的文件的ProjectItem添加到al中
Private Sub AppendToArrayList(ByVal al As ArrayList, ByVal items As ProjectItems, ByVal ext As String)
Dim item As ProjectItem
For Each item In items
If item.Kind = Constants.vsProjectItemKindPhysicalFile And item.Name.ToLower().EndsWith(ext) Then
DTE.StatusBar.Text = item.Properties.Item("FullPath").Value
al.Add(item)
End If
AppendToArrayList(al, item.ProjectItems, ext)
Next
End Sub