Public Sub AttachToWebServer()
Dim attached As Boolean = False
Dim project As EnvDTE.Project = GetStartupProject()
If (project Is Nothing) Then
MsgBox("Couldn't find a web project that can be attached")
Return
End If
attached = AttachToProcess(project)
If (Not attached) Then
MsgBox("Couldn't attach to the process")
End If
End Sub
Private Function GetStartupProject() As EnvDTE.Project
Dim startUpProject As String = DTE.Solution.Properties.Item("StartupProject").Value
For Each currentProject As EnvDTE.Project In DTE.Solution.Projects
If currentProject.Name = startUpProject Then
Return currentProject
End If
Next
Return Nothing
End Function
Private Function AttachToProcess(ByVal project As EnvDTE.Project) As Boolean
Dim serverProcessNamePattern As String
If project.Properties.Item("WebApplication.UseIIS").Value = "True" Then
If project.Properties.Item("WebApplication.DevelopmentServerCommandLine").Value.ToString().Length > 0 Then
serverProcessNamePattern = ".*iisexpress.exe"
Else
serverProcessNamePattern = ".*w3wp.exe"
End If
Else
serverProcessNamePattern = ".*WebDev.WebServer\d+.EXE"
End If
Return AttachToWebServer(serverProcessNamePattern)
End Function
Private Function AttachToWebServer(ByVal serverProcessNamePattern As String) As Boolean
Dim attached As Boolean = False
For Each process In DTE.Debugger.LocalProcesses
If (Regex.IsMatch(process.Name, serverProcessNamePattern)) Then
process.Attach()
attached = True
Exit For
End If
Next
Return attached
End Function