3

Is it possible to get the process id of a visual studio instance through the DTE mDte variable? Refer to code below.

    private static DTE mDte;

    public static void OpenVisualStudio()
    {
        Type visualStudioType = Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
        mDte = Activator.CreateInstance(visualStudioType) as DTE;

        if (mDte != null)
        {
            mDte.MainWindow.Visible = true;
        }

        // get process id of visual studio instance through mDte
    }
Lasse
  • 51
  • 5

2 Answers2

0

I have done as follows :

    public static int OpenVisualStudio()
    {
        var devenv = Process.Start("devenv.exe");

        if (devenv == null)
        {
            return 0;
        }

        do
        {
            System.Threading.Thread.Sleep(2000);
            mDte = GetDte(devenv.Id);
        }
        while (mDte == null);

        return devenv.Id;
    }

I got it from here: http://blogs.msdn.com/b/kirillosenkov/archive/2011/08/10/how-to-get-dte-from-visual-studio-process-id.aspx

It solves my problem for now...

Lasse
  • 51
  • 5
-1

The DTE object has a Debugger property which has a CurrentProcess property which has a ProcessID property.

int processId = dte.Debugger.CurrentProcess.ProcessID;
Rami A.
  • 10,302
  • 4
  • 44
  • 87
  • 1
    dte.Debugger.CurrentProcess is not the process of the debugger (i.e. a devenv), it's the process that is currently being debugged. – Alex Feb 22 '17 at 21:00