Click here to Skip to main content
Click here to Skip to main content

Automate the "attach to process"

, 25 Oct 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Automating the Attach to Process from within Visual Studio.


Often you can catch yourself doing repetitive work. Sometimes I even manage to get annoyed before I realize what the problem is. This article is about my latest idea: automating the "attach to process" from Visual Studio.


Developing a client/server solution will often bring you in a situation where you start debugging the client and then realize that there is a problem with the server, so you start the client (again) and attach Visual Studio to the server in order to debug the problem. After a while, this routine becomes really annoying. While reading through some of the Microsoft pages regarding EnvDTE, I had an idea. The idea is basically to get hold of the running instance of Visual Studio from the client and use EvnDTE to attach to the server.

Using the Code

I created an extension method to extend the System.Diagnostics.Process. This seemed rational since I had to use a Process to start the server from the client.

The code is as follows:

public static void Attach(this System.Diagnostics.Process process)
  // Reference Visual Studio core
  DTE dte;
    dte = (DTE) Marshal.GetActiveObject(progId); 
  } catch (COMException) {
    Debug.WriteLine(String.Format(@"Visual studio not found."));
  // Try loop - Visual Studio may not respond the first time.
  int tryCount = 5;
  while (tryCount-- > 0)
      Processes processes = dte.Debugger.LocalProcesses;
      foreach (Process proc in processes.Cast<Process>().Where(
        proc => proc.Name.IndexOf(process.ProcessName) != -1))
		("Attached to process {0} successfully.", process.ProcessName));
    catch (COMException)

Using the extension method can be done like this (example):

Process p = new Process();
FileInfo path = new FileInfo(Assembly.GetExecutingAssembly().Location);
string pathToProcessToDebug = Path.Combine
    (path.Directory.FullName, @"..\..\..\ProcessToDebug\Bin\Debug\ProcessToDebug.exe");
ProcessStartInfo psi = new ProcessStartInfo(pathToProcessToDebug);
p.StartInfo = psi;
p.Attach(); // <-- Extension method 

Points of Interest

The idea could be implemented in any number of ways, but I was keen on using an extension method. I looked at the System.Diagnistics.Debug class for an appropriate place to add the method - but static classes cannot be extended.


  • Version 1.0 (First version).


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Niel M.Thomas
Software Developer (Senior)
Denmark Denmark
Name: Niel Morgan Thomas
Born: 1970 in Denmark
Dataengineer from Odense Technical University.
More than 20 years in IT-business.
Current employment:
Working with application development in a major Danish company that produce medical equipment.

Comments and Discussions

Questionwhat is progId Pinprofessionalvikas.yerram22-Nov-14 20:32 
AnswerRe: what is progId PinprofessionalNiel M.Thomas22-Nov-14 22:23 
GeneralRe: what is progId Pinprofessionalvikas.yerram23-Nov-14 16:59 
QuestionCould any one tell me the simplest step to use it? Pinmemberleiyangge16-Jan-14 5:30 
AnswerRe: Could any one tell me the simplest step to use it? PinmemberNiel M.Thomas16-Jan-14 5:35 
GeneralRe: Could any one tell me the simplest step to use it? Pinmemberleiyangge16-Jan-14 15:32 
QuestionThis may help too PinmemberMauro Leggieri5-Oct-11 16:19 
GeneralMy vote of 5 PinmemberShdNx4-Oct-11 6:03 
GeneralMy vote of 5 PinmemberGPUToaster™4-Oct-11 0:22 
GeneralMy vote of 5 PinmemberHoyaSaxa9330-Sep-11 11:43 
GeneralRe: My vote of 5 Pinmemberkiran dangar4-Oct-11 1:36 
QuestionImpeccable timing guys Pinmemberrocware2129-Sep-11 14:25 
Suggestionseems a bit excessive to me PinmemberBlkbam29-Sep-11 8:19 
GeneralRe: seems a bit excessive to me PinmemberNiel M.Thomas29-Sep-11 9:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150302.1 | Last Updated 25 Oct 2013
Article Copyright 2011 by Niel M.Thomas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid