/// <summary> /// Rename document in the running document table from oldName to newName. /// </summary> /// <param name="provider">The service provider.</param> /// <param name="oldName">Full path to the old name of the document.</param> /// <param name="newName">Full path to the new name of the document.</param> /// <param name="newItemId">The new item id of the document</param> public static void RenameDocument(IServiceProvider site, string oldName, string newName, uint newItemId) { Utilities.ArgumentNotNull("site", site); if (String.IsNullOrEmpty(oldName)) { throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty), "oldName"); } if (String.IsNullOrEmpty(newName)) { throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty), "newName"); } if (newItemId == VSConstants.VSITEMID_NIL) { throw new ArgumentNullException("newItemId"); } IVsRunningDocumentTable pRDT = site.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable; IVsUIShellOpenDocument doc = site.GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument; if (pRDT == null || doc == null) { return; } IVsHierarchy pIVsHierarchy; uint itemId; IntPtr docData; uint uiVsDocCookie; ErrorHandler.ThrowOnFailure(pRDT.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, oldName, out pIVsHierarchy, out itemId, out docData, out uiVsDocCookie)); if (docData != IntPtr.Zero && pIVsHierarchy != null) { try { IntPtr pUnk = Marshal.GetIUnknownForObject(pIVsHierarchy); Guid iid = typeof(IVsHierarchy).GUID; IntPtr pHier; Marshal.QueryInterface(pUnk, ref iid, out pHier); try { ErrorHandler.ThrowOnFailure(pRDT.RenameDocument(oldName, newName, pHier, newItemId)); } finally { if (pHier != IntPtr.Zero) { Marshal.Release(pHier); } if (pUnk != IntPtr.Zero) { Marshal.Release(pUnk); } } } finally { Marshal.Release(docData); } } }
public static void RenameDocument(ServiceProvider site, string oldName, string newName) { IVsRunningDocumentTable pRDT = (IVsRunningDocumentTable)site.QueryService(VsConstants.SID_SVsRunningDocumentTable, typeof(IVsRunningDocumentTable)); IVsUIShellOpenDocument doc = (IVsUIShellOpenDocument)site.QueryService(VsConstants.SID_VsUIShellOpenDocument, typeof(IVsUIShellOpenDocument)); IVsUIShell uiShell = (IVsUIShell)site.QueryService(VsConstants.guidShellIID, typeof(IVsUIShell)); if (pRDT == null || doc == null) { return; } IVsHierarchy pIVsHierarchy; uint itemId; IntPtr docData; uint uiVsDocCookie; pRDT.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, oldName, out pIVsHierarchy, out itemId, out docData, out uiVsDocCookie); if (docData != IntPtr.Zero) { IntPtr pUnk = Marshal.GetIUnknownForObject(pIVsHierarchy); Guid iid = typeof(IVsHierarchy).GUID; IntPtr pHier; Marshal.QueryInterface(pUnk, ref iid, out pHier); try { pRDT.RenameDocument(oldName, newName, pHier, itemId); } finally { Marshal.Release(pHier); Marshal.Release(pUnk); } string newCaption = Path.GetFileName(newName); // now we need to tell the windows to update their captions. IEnumWindowFrames ppenum; uiShell.GetDocumentWindowEnum(out ppenum); IVsWindowFrame[] rgelt = new IVsWindowFrame[1]; uint fetched; while (ppenum.Next(1, rgelt, out fetched) == 0 && fetched == 1) { IVsWindowFrame windowFrame = rgelt[0]; object data; windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out data); IntPtr ptr = Marshal.GetIUnknownForObject(data); if (ptr == docData) { windowFrame.SetProperty((int)__VSFPROPID.VSFPROPID_OwnerCaption, newCaption); } Marshal.Release(ptr); } Marshal.Release(docData); } }
/// <summary> /// Rename document in the running document table from oldName to newName. /// </summary> /// <param name="site">The service provider.</param> /// <param name="oldName">Full path to the old name of the document.</param> /// <param name="newName">Full path to the new name of the document.</param> /// <param name="newItemId">The new item id of the document</param> public static void RenameDocument(IServiceProvider site, string oldName, string newName, uint newItemId) { if (site == null) { throw new ArgumentNullException("site"); } if (String.IsNullOrEmpty(oldName)) { throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty, CultureInfo.CurrentUICulture), "oldName"); } if (String.IsNullOrEmpty(newName)) { throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty, CultureInfo.CurrentUICulture), "newName"); } if (newItemId == VSConstants.VSITEMID_NIL) { throw new ArgumentNullException("newItemId"); } IVsRunningDocumentTable pRDT = site.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable; if (pRDT == null) { return; } IVsHierarchy pIVsHierarchy; uint itemId; IntPtr docData = IntPtr.Zero; uint uiVsDocCookie; try { int hr = pRDT.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, oldName, out pIVsHierarchy, out itemId, out docData, out uiVsDocCookie); ErrorHandler.ThrowOnFailure(hr); if (pIVsHierarchy == null) { // the doc is not in the RDT yet, e.g. user never opened this doc. // nothing to do then. } else { IntPtr pUnk = Marshal.GetIUnknownForObject(pIVsHierarchy); Guid iid = typeof(IVsHierarchy).GUID; IntPtr pHier; Marshal.QueryInterface(pUnk, ref iid, out pHier); try { ErrorHandler.ThrowOnFailure(pRDT.RenameDocument(oldName, newName, pHier, newItemId)); } finally { if (pHier != IntPtr.Zero) { Marshal.Release(pHier); } if (pUnk != IntPtr.Zero) { Marshal.Release(pUnk); } } } } finally { if (docData != IntPtr.Zero) { Marshal.Release(docData); } } }
private void VerifySolutionNaming() { ThreadHelper.ThrowIfNotOnUIThread(); IVsSolution sol = GetService <IVsSolution>(typeof(SVsSolution)); string dir, path, user; if (sol == null || !VSErr.Succeeded(sol.GetSolutionInfo(out dir, out path, out user)) || string.IsNullOrEmpty(path)) { return; } string trueSln = SvnTools.GetTruePath(path, true) ?? SvnTools.GetNormalizedFullPath(path); if (trueSln == path) { return; // Nothing to do for us } IVsRunningDocumentTable rdt = GetService <IVsRunningDocumentTable>(typeof(SVsRunningDocumentTable)); if (rdt == null) { return; } Guid IID_hier = typeof(IVsHierarchy).GUID; IntPtr hier = IntPtr.Zero; IntPtr unk = Marshal.GetIUnknownForObject(sol); IntPtr ppunkDocData = IntPtr.Zero; try { IVsHierarchy slnHier; uint pitemid; uint pdwCookie; if (!VSErr.Succeeded(rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_EditLock, path, out slnHier, out pitemid, out ppunkDocData, out pdwCookie))) { return; } if (!VSErr.Succeeded(Marshal.QueryInterface(unk, ref IID_hier, out hier))) { hier = IntPtr.Zero; return; } if (VSErr.Succeeded(rdt.RenameDocument(path, trueSln, hier, VSItemId.Root))) { int hr; hr = rdt.SaveDocuments((uint)(__VSRDTSAVEOPTIONS.RDTSAVEOPT_ForceSave | __VSRDTSAVEOPTIONS.RDTSAVEOPT_SaveNoChildren), slnHier, pitemid, pdwCookie); hr = sol.SaveSolutionElement((uint)(__VSSLNSAVEOPTIONS.SLNSAVEOPT_ForceSave), (IVsHierarchy)sol, pdwCookie); //GC.KeepAlive(hr); } if (ppunkDocData != IntPtr.Zero) { object doc = Marshal.GetObjectForIUnknown(ppunkDocData); } } finally { System.Runtime.InteropServices.Marshal.Release(unk); if (hier != IntPtr.Zero) { System.Runtime.InteropServices.Marshal.Release(hier); } if (ppunkDocData != IntPtr.Zero) { Marshal.Release(hier); } } }