/// <summary> /// This starts an instance of Visual Studio and get its DTE object. /// </summary> /// <returns>DTE of the started instance.</returns> public static DTE2 StartVisualStudioInstance() { // Set up filter to handle threading events and automatically retry calls // to dte which fail because dte is busy. ComMessageFilter.Register(); Type visualStudioType; if (IsVS2012()) { visualStudioType = Type.GetTypeFromProgID("VisualStudio.DTE.11.0"); } else { visualStudioType = Type.GetTypeFromProgID("VisualStudio.DTE.10.0"); } DTE2 visualStudio = Activator.CreateInstance(visualStudioType) as DTE2; if (visualStudio == null) { throw new Exception("Visual Studio failed to start"); } visualStudio.MainWindow.Visible = true; return(visualStudio); }
/// <summary> /// This properly cleans up after StartVisualStudioInstance(). /// </summary> /// <param name="dte">Dte instance returned by StartVisualStudioInstance().</param> public static void CleanUpVisualStudioInstance(DTE2 dte) { if (dte != null) { if (dte.Solution != null) { dte.Solution.Close(); } dte.Quit(); } // Stop the message filter. ComMessageFilter.Revoke(); }