Gokul's Blog

Macro to attach a process to Debug in Visual Studio

Leave a comment

When we have to attach a process for debugging in Visual Studio we have to manually do it by navigating to the Debug menu ==> attach to process.

Here is the code which I tweaked a bit to automate this process.
Reference Url: http://consultingblogs.emc.com/howardvanrooijen/archive/2007/06/24/Attach-to-Web-Server-Macro-for-Visual-Studio.aspx

Code (this enhanced code also supports the Visual Studio built in cassini server)

Public Module AttachProcesstoDebug

    Public Sub AttachToWebServer()

        Dim AspNetWp As String = "aspnet_wp.exe"
        Dim W3WP As String = "w3wp.exe"
        Dim Webdev As String = "webdev.webserver.exe"
        If Not (AttachToProcess(Webdev)) Then
            If Not (AttachToProcess(AspNetWp)) Then
                If Not AttachToProcess(W3WP) Then
                    System.Windows.Forms.MessageBox.Show(String.Format("Process {0} or {1} Cannot Be Found", AspNetWp, W3WP), "Attach To Web Server Macro")
                End If
            End If
        End If
    End Sub

    Public Function AttachToProcess(ByVal ProcessName As String) As Boolean

        Dim Processes As EnvDTE.Processes = DTE.Debugger.LocalProcesses
        Dim Process As EnvDTE.Process
        Dim ProcessFound As Boolean = False

        For Each Process In Processes
            If (Process.Name.Substring(Process.Name.LastIndexOf("\") + 1).ToLower() = ProcessName.ToLower()) Then
                Process.Attach()
                ProcessFound = True
            End If
        Next

        AttachToProcess = ProcessFound

    End Function
End Module

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s