public static bool NavigateToFile(Uri fileUri, int start, int length, bool allowProvisionalTab) { if (fileUri == null || !fileUri.IsAbsoluteUri || !fileUri.IsFile) { Debug.Fail("Invalid fileUri: " + (fileUri != null ? fileUri.ToString() : string.Empty)); return(false); } string localPath = fileUri.LocalPath; if (!File.Exists(localPath)) { Debug.Fail("File doesn't exist: " + localPath); return(false); } __VSNEWDOCUMENTSTATE newState = allowProvisionalTab ? __VSNEWDOCUMENTSTATE.NDS_Provisional : __VSNEWDOCUMENTSTATE.NDS_Permanent; using (new NewDocumentStateScope(newState, VSConstants.NewDocumentStateReason.Navigation)) { Guid logicalViewGuid = VSConstants.LOGVIEWID.TextView_guid; Microsoft.VisualStudio.OLE.Interop.IServiceProvider serviceProvider; IVsUIHierarchy hierarchy; IVsWindowFrame frame; uint itemId; IVsUIShellOpenDocument openService = VsAppShell.Current.GetGlobalService <IVsUIShellOpenDocument>(typeof(SVsUIShellOpenDocument)); if (openService != null) { int hr = openService.OpenDocumentViaProject( localPath, ref logicalViewGuid, out serviceProvider, out hierarchy, out itemId, out frame); if (ErrorHandler.Succeeded(hr) && frame != null) { return(NavigateToFrame(frame, start, length)); } } } return(false); }
public NewDocumentStateScope(__VSNEWDOCUMENTSTATE state, Guid reason) : this((uint)state, reason) { }