private void CloseAndOpenInXMLEditor(string filePath) { IVsHierarchy ppHier = null; uint pitemid = Microsoft.VisualStudio.VSConstants.VSITEMID_NIL; IntPtr ppunkDocData = IntPtr.Zero; uint pdwCookie = Microsoft.VisualStudio.VSConstants.VSITEMID_NIL; try { // Get the IVsRunningDocumentTable interface and cast it to // IVsRunningDocumentTable2 interface. IVsRunningDocumentTable rdt = GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable; IVsRunningDocumentTable2 rdt2 = rdt as IVsRunningDocumentTable2; // Find the opened document(.rc file) from the RDT. rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, filePath, out ppHier, out pitemid, out ppunkDocData, out pdwCookie); if (ppunkDocData != IntPtr.Zero) { // Close the opened document. Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure( rdt2.CloseDocuments((uint)__FRAMECLOSE.FRAMECLOSE_SaveIfDirty, null, pdwCookie)); ppunkDocData = IntPtr.Zero; } IVsInvisibleEditorManager spIEM; IVsInvisibleEditor invisibleEditor = null; IVsWindowFrame winFrame = null; Guid logicalView = Microsoft.VisualStudio.VSConstants.LOGVIEWID_Primary; // Get the IVsInvisibleEditorManager interface. spIEM = (IVsInvisibleEditorManager) GetService(typeof(IVsInvisibleEditorManager)); // Register a invisible editor, then the specific document will be // loaded into the RDT. spIEM.RegisterInvisibleEditor(filePath, null, (uint)_EDITORREGFLAGS.RIEF_ENABLECACHING, null, out invisibleEditor); // Get the IVsUIShellOpenDocument interface. IVsUIShellOpenDocument uiShellOpenDocument = GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument; // Guid of the Microsoft XML editor Guid guidXMLEditor = new Guid("{FA3CD31E-987B-443A-9B81-186104E8DAC1}"); rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, filePath, out ppHier, out pitemid, out ppunkDocData, out pdwCookie); // Open the document in XML editor. Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure( uiShellOpenDocument.OpenSpecificEditor((uint)0, filePath, ref guidXMLEditor, "", ref logicalView, "XML Editor", ppHier as IVsUIHierarchy, pitemid, ppunkDocData, this, out winFrame)); // Show the editor window. winFrame.Show(); } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); } }
/// <summary> /// Constructor stores DataViewHierarchyAccessor reference in the private variable. /// </summary> /// <param name="hierarchy">DataViewHierarchyAccessor reference.</param> public ServerExplorerFacade(DataViewHierarchyAccessor hierarchy) { if (hierarchy == null) throw new ArgumentNullException("hierarchy"); if (hierarchy.Connection == null) throw new ArgumentException(Resources.Error_InvalidAccessorNoConnection, "hierarchy"); // Store hierarchy accessor reference hierarchyAccessor = hierarchy; //Extract connection wrapper connectionWrapper = new DataConnectionWrapper(hierarchy.Connection); // Get UI shell object uiShell = Package.GetGlobalService(typeof(IVsUIShell)) as IVsUIShell; Debug.Assert(uiShell != null, "Unable to get UI shell"); if (uiShell == null) throw new Exception(Resources.Error_UnableToGetUIShell); // Retrieve IVsUIShellOpenDocument interface shell = Package.GetGlobalService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument; Debug.Assert(shell != null, "Unable to get IVsUIShellOpenDocument reference!"); if (shell == null) throw new Exception(Resources.Error_UnableToGetOpenDocumentShell); // Get RDT interface rdt = Package.GetGlobalService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable; Debug.Assert(rdt != null, "Unable to get Running Document Table interface reference!"); if (rdt == null) throw new Exception(Resources.Error_UnableToGetRdt); // Get RDT2 interface rdt2 = rdt as IVsRunningDocumentTable2; Debug.Assert(rdt2 != null, "Unable to get Running Document Table extended interface reference!"); if (rdt2 == null) throw new Exception(Resources.Error_UnableToGetRdt); }