Table of Contents

Visual Studio Macros

Reformat ASPX

    Sub ReformatAspx()
        Dim tags() As String = { _
            "tr", "/tr", "td", "/td", "th", "/th", "tbody", "/tbody", "thead", "/thead", "table", "/table", "form", "/form", "link", _
            "div", "/div", "img", "head", "/head", "body", "/body", "html", "/html", _
            "uc1:", "ccc:", "asp:literal", _
            "asp:textbox", "asp:requiredfieldvalidator", "asp:customvalidator", "asp:button", "asp:radiobutton", "ccc:fieldlabel" _
        }
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = False
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
 
        For Each tag As String In tags
            DTE.Find.FindWhat = "<" + tag
            DTE.Find.ReplaceWith = vbNewLine + "<" + tag
            DTE.Find.Execute()
        Next tag
 
        DTE.ExecuteCommand("Edit.FormatDocument")
 
        DTE.Find.FindWhat = vbNewLine + vbNewLine
        DTE.Find.ReplaceWith = vbNewLine
        DTE.Find.Execute()
        DTE.Find.Execute()
        DTE.Find.Execute()
        DTE.Find.Execute()
 
    End Sub

Super Mega Reset

    Sub SuperMegaReset()
        Dim objProcess As System.Diagnostics.Process
        Dim objProject As Project
        Dim PropObj As [Property]
        Dim fullPath As String
        Dim missedDirectories As String
        Dim deleteDirs() = New String() {"bin", "obj"}
        Dim deleteDir As String
 
        DTE.StatusBar.Text = "Killing ASP.NET worker process"
 
        For Each objProcess In System.Diagnostics.Process.GetProcessesByName("aspnet_wp")
            objProcess.Kill()
        Next
 
        For Each objProject In DTE.Solution.Projects
            If Not IsNothing(objProject.Properties) Then
                For Each PropObj In objProject.Properties
                    DTE.StatusBar.Text = "[" & objProject.Name & "] " & PropObj.Name
 
                    If PropObj.Name = "FullPath" Then
                        For Each deleteDir In deleteDirs
 
                            fullPath = PropObj.Value & deleteDir
 
                            If System.IO.Directory.Exists(fullPath) Then
                                Try
                                    DTE.StatusBar.Text = "Deleting " & fullPath
                                    System.IO.Directory.Delete(fullPath, True)
                                Catch ex As System.IO.IOException
                                    missedDirectories = missedDirectories & Microsoft.VisualBasic.ControlChars.CrLf & Microsoft.VisualBasic.ControlChars.CrLf _
                                        & "[" & objProject.Name & "] " & fullPath & Microsoft.VisualBasic.ControlChars.CrLf _
                                        & "    (" & ex.Message & ")"
                                End Try
                            End If
                        Next
                    End If
                Next
            End If
        Next
 
        DTE.StatusBar.Text = "Rebuilding"
 
        'Rebuild the entire Solution
        DTE.ExecuteCommand("Build.RebuildSolution")
 
        If missedDirectories <> "" Then
            MsgBox("The following directories were not deleted:" & missedDirectories)
        End If
 
    End Sub

Text to C# String Assignment

    Sub TextToCSharpStringAssignment()
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
        DTE.ActiveDocument.Selection.EndOfLine(True)
 
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocumentSelection
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = False
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
 
        DTE.Find.FindWhat = vbTab
        DTE.Find.ReplaceWith = "\t"
        DTE.Find.Execute()
 
        DTE.Find.FindWhat = """"
        DTE.Find.ReplaceWith = "\"""
        DTE.Find.Execute()
 
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
        DTE.ActiveDocument.Selection.Indent()
        DTE.ActiveDocument.Selection.Indent()
        DTE.ActiveDocument.Selection.Text = """"
        DTE.ActiveDocument.Selection.EndOfLine()
        DTE.ActiveDocument.Selection.Text = "\n"" +"
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
        DTE.ActiveDocument.Selection.LineDown()
    End Sub

Kill ASP.Net Worker Process

    Sub KillAspNetWorkerProcess()
        Dim objProcess As System.Diagnostics.Process
 
        DTE.StatusBar.Text = "Gathering system process information."
 
        For Each objProcess In System.Diagnostics.Process.GetProcessesByName("aspnet_wp")
            If Not objProcess.HasExited Then
                Try
                    DTE.StatusBar.Text = "Killing process " + objProcess.Id.ToString()
 
                    objProcess.Kill()
 
                Catch ex As System.Threading.ThreadAbortException
                    ' Just ignore it.
                End Try
            End If
        Next
 
        DTE.StatusBar.Text = "ASP.Net worker process killed."
 
    End Sub