Attaching debugger at runtime in .Net

Whenever you write code that starts a new Process, your debugging experience is rather lacking – you’re stuck outside of the debugger. The following extension method will attach the Visual Studio debugger to your process:

using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using EnvDTE;
using Process = EnvDTE.Process;

namespace Sample
{
    ///<summary>
    /// Class ProcessExtender
    /// Attach to other processes in order to debug.
    ///</summary>
    public static class ProcessExtender
    {
        conststring progId = @"VisualStudio.DTE.11.0";
        //const string progId = @"VisualStudio.DTE.9.0"; // Vs2008

        #region -- Public Methods --
        ///summary
        /// Attaches Visual Studio (2010) to the specified process.
        ////summary
        ///param name=processThe process./param
        public static void Attach(this System.Diagnostics.Process process)
        {
            // Reference visual studio core
            DTE dte;
            try
            {
                dte = (DTE)Marshal.GetActiveObject(progId);
            }
            catch (COMException)
            {
                Debug.WriteLine(String.Format(@"Visual studio not found."));
                return;
            }

            // Try loop - visual studio may not respond the first time.
            int tryCount = 5;
            while (tryCount--  0)
            {
                try
                {
                    Processes processes = dte.Debugger.LocalProcesses;
                    foreach (Process proc in processes.CastProcess().Where(
                        proc = proc.Name.IndexOf(process.ProcessName) != -1))
                    {
                        proc.Attach();
                        Debug.WriteLine(String.Format("Attached to process {0} successfully.", process.ProcessName));
                        break;
                    }
                    break;
                }
                catch (COMException)
                {
                    System.Threading.Thread.Sleep(1000);
                }
            }
        }
        #endregion
    }
}