Assuming you know which instance / version of Visual Studio is connected to your process, you can detach it as follows:
object dt = Marshal.GetActiveObject("VisualStudio.DTE.12.0") DTE dte = (DTE)dt; dte.Debugger.DetachAll();
"12" for version 2013 of Visual Studio. For a different version, change accordingly. This requires a link to EnvDTE, which is usually located in the C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Common7 \ IDE \ PublicAssemblies \ EnvDTE.dll folder
This will disconnect ALL processes from this instance of Visual Studio, so if for any reason Visual Studio will be attached to other processes in addition to yours, these processes will also be disabled. In addition, you may get unexpected results if you have multiple instances of Visual Studio open.
To be careful only to separate the current process and only the correct instance of Visual Studio, use the following code:
using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; using EnvDTE; public class Test { public static void DetachCurrentProcesses() { System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("devenv"); DTE dte = null; foreach (System.Diagnostics.Process devenv in procs) { do { System.Threading.Thread.Sleep(2000); dte = AutomateVS.GetDTE(devenv.Id); } while (dte == null); IEnumerable<Process> processes = dte.Debugger.DebuggedProcesses.OfType<Process>(); if (!processes.Any) continue; int currentID = System.Diagnostics.Process.GetCurrentProcess().Id; processes.Where(p => p.ProcessID == currentID).ToList.ForEach(p => p.Detach(false)); Marshal.ReleaseComObject(dte); } } }
source share