public void CloseCurrentSolution(__VSSLNSAVEOPTIONS saveoptions) { // Get solution service IVsSolution solutionService = (IVsSolution)VsIdeTestHostContext.ServiceProvider.GetService(typeof(IVsSolution)); // Close already open solution solutionService.CloseSolutionElement((uint)saveoptions, null, 0); }
public static void CloseCurrentSolution(__VSSLNSAVEOPTIONS saveoptions) { // Get solution service IVsSolution solutionService = (IVsSolution)VsIdeTestHostContext.ServiceProvider.GetService(typeof(IVsSolution)); // Close already open solution solutionService.CloseSolutionElement((uint)saveoptions, null, 0); }
/// <summary> /// Saves just the project file (does not save dirty documents) /// </summary> int SaveProject(IReloadableProject project) { IVsSolution solution = _serviceProvider.GetService <IVsSolution, SVsSolution>(); __VSSLNSAVEOPTIONS saveOpts = __VSSLNSAVEOPTIONS.SLNSAVEOPT_SkipDocs; VsShellUtilities.GetRDTDocumentInfo(_serviceProvider, project.ProjectFile, out IVsHierarchy hier, out uint itemid, out IVsPersistDocData docData, out uint docCookie); int hr = solution.SaveSolutionElement((uint)saveOpts, project.VsHierarchy, docCookie); return(hr); }
/// <summary> /// 关闭一个已经打开的文档 /// </summary> /// <param name="name">文档路径(包含文件后缀)</param> /// <param name="saveOption">制定如何关闭一个文档,默认为强制保存后关闭</param> public static void CloseDocument(string name, __VSSLNSAVEOPTIONS saveOption = __VSSLNSAVEOPTIONS.SLNSAVEOPT_ForceSave) { IVsRunningDocumentTable runningTabs = RunningDocumentTableObject; IVsSolution solution = VSSolution.SolutionObject; if (runningTabs != null) { IEnumRunningDocuments runningDocuments; runningTabs.GetRunningDocumentsEnum(out runningDocuments); IntPtr documentData = IntPtr.Zero; uint[] docCookie = new uint[1]; uint fetched; //遍历所有已经打开的文档 while ((VSConstants.S_OK == runningDocuments.Next(1, docCookie, out fetched)) && (fetched == 1)) { uint flags; uint editLocks; uint readLocks; string filePath; IVsHierarchy docHierarchy; uint docId; IntPtr docData = IntPtr.Zero; try { ErrorHandler.ThrowOnFailure(runningTabs.GetDocumentInfo(docCookie[0], out flags, out readLocks, out editLocks, out filePath, out docHierarchy, out docId, out docData)); if (solution != null && !filePath.EndsWith("sln") && !filePath.EndsWith("csproj")) { //string currentName = filePath.Substring(filePath.LastIndexOf("\\") + 1); if (name == filePath) { solution.CloseSolutionElement((uint)saveOption, docHierarchy, docCookie[0]); } } } finally { if (docData != IntPtr.Zero) { Marshal.Release(docData); } } } } }
/// <summary> /// 关闭一个已经打开的文档 /// </summary> /// <param name="name">文档路径(包含文件后缀)</param> /// <param name="saveOption">制定如何关闭一个文档,默认为强制保存后关闭</param> public static void CloseDocument(string name, __VSSLNSAVEOPTIONS saveOption = __VSSLNSAVEOPTIONS.SLNSAVEOPT_ForceSave) { IVsRunningDocumentTable runningTabs = RunningDocumentTableObject; IVsSolution solution = VSSolution.SolutionObject; if (runningTabs != null) { IEnumRunningDocuments runningDocuments; runningTabs.GetRunningDocumentsEnum(out runningDocuments); IntPtr documentData = IntPtr.Zero; uint[] docCookie = new uint[1]; uint fetched; //遍历所有已经打开的文档 while ((VSConstants.S_OK == runningDocuments.Next(1, docCookie, out fetched)) && (fetched == 1)) { uint flags; uint editLocks; uint readLocks; string filePath; IVsHierarchy docHierarchy; uint docId; IntPtr docData = IntPtr.Zero; try { ErrorHandler.ThrowOnFailure(runningTabs.GetDocumentInfo(docCookie[0], out flags, out readLocks, out editLocks, out filePath, out docHierarchy, out docId, out docData)); if (solution != null && !filePath.EndsWith("sln") && !filePath.EndsWith("csproj")) { //string currentName = filePath.Substring(filePath.LastIndexOf("\\") + 1); if (name == filePath) { solution.CloseSolutionElement((uint)saveOption, docHierarchy, docCookie[0]); } } } finally { if (docData != IntPtr.Zero) { Marshal.Release(docData); } } } } }
public static bool SaveSolution(IServiceProvider serviceProvider, bool silent) { if (serviceProvider == null) { throw new ArgumentNullException(nameof(serviceProvider)); } var solutionService = (IVsSolution)serviceProvider.GetService(typeof(SVsSolution)); __VSSLNSAVEOPTIONS saveOptions = __VSSLNSAVEOPTIONS.SLNSAVEOPT_SaveIfDirty; if (!silent) { saveOptions |= __VSSLNSAVEOPTIONS.SLNSAVEOPT_PromptSave; } int hr = solutionService.SaveSolutionElement((uint)saveOptions, null, 0); // True if user clicked Yes, false otherwise (No/Cancel/Esc/Close dialog) return(hr != VSConstants.E_ABORT && ErrorHandler.ThrowOnFailure(hr) == VSConstants.S_OK); }