public override void CloseView(bool force) { if (!force && IsDirty) { ResourceService resourceService = (ResourceService)ServiceManager.Services.GetService(typeof(ResourceService)); DialogResult dr = MessageBox.Show(resourceService.GetString("MainWindow.SaveChangesMessage"), resourceService.GetString("MainWindow.SaveChangesMessageHeader") + " ?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); switch (dr) { case DialogResult.Yes: if (ContentName == null) { SaveFileAs(); if (IsDirty) { return; //如果用户在保存新建的文件时选择了取消按钮,则直接返回,并且不关闭当前文档. } } else { FileUtilityService fileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService)); fileUtilityService.ObservedSave(new FileOperationDelegate(SaveFile), ContentName, FileErrorPolicy.ProvideAlternative); } break; case DialogResult.No: break; case DialogResult.Cancel: return; } } OnCloseEvent(null); }
public override void Run() { IViewContent content = WorkbenchSingleton.Workbench.ActiveViewContent; if (content != null) { if (content.IsViewOnly) { return; } if (content.ContentName == null) { SaveFileAs sfa = new SaveFileAs(); sfa.Run(); } else { FileAttributes attr = FileAttributes.ReadOnly | FileAttributes.Directory | FileAttributes.Offline | FileAttributes.System; if ((File.GetAttributes(content.ContentName) & attr) != 0) { SaveFileAs sfa = new SaveFileAs(); sfa.Run(); } else { FileUtilityService fileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService)); fileUtilityService.ObservedSave(new FileOperationDelegate(content.SaveFile), content.ContentName); } } } }
public override void Run() { FileUtilityService fileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService)); foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection) { if (content.IsViewOnly) { continue; } if (content.ContentName == null) { using (SaveFileDialog fdiag = new SaveFileDialog()) { fdiag.OverwritePrompt = true; fdiag.AddExtension = true; fdiag.Filter = String.Join("|", (string[])(AddInTreeSingleton.AddInTree.GetTreeNode("/DataStructure/Workbench/FileFilter").BuildChildItems(this)).ToArray(typeof(string))); if (fdiag.ShowDialog() == DialogResult.OK) { string fileName = fdiag.FileName; if (Path.GetExtension(fileName).StartsWith("?") || Path.GetExtension(fileName) == "*") { fileName = Path.ChangeExtension(fileName, ""); } if (fileUtilityService.ObservedSave(new NamedFileOperationDelegate(content.SaveFile), fileName) == FileOperationResult.OK) { //MessageBox.Show(fileName, "文件已保存", MessageBoxButtons.OK); } } } } else { fileUtilityService.ObservedSave(new FileOperationDelegate(content.SaveFile), content.ContentName); } } }
static void WriteToolsToFile(string fileName) { XmlDocument doc = new XmlDocument(); doc.LoadXml("<TOOLS VERSION = \"" + TOOLFILEVERSION + "\" />"); foreach (ExternalTool et in tool) { doc.DocumentElement.AppendChild(et.ToXmlElement(doc)); } FileUtilityService fileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService)); fileUtilityService.ObservedSave(new NamedFileOperationDelegate(doc.Save), fileName, FileErrorPolicy.ProvideAlternative); }
public override void Run() { IViewContent content = WorkbenchSingleton.Workbench.ActiveViewContent; if (content != null) { if (content.IsViewOnly) { return; } using (SaveFileDialog fdiag = new SaveFileDialog()) { fdiag.OverwritePrompt = true; fdiag.AddExtension = true; string[] fileFilters = (string[])(AddInTreeSingleton.AddInTree.GetTreeNode("/DataStructure/Workbench/FileFilter").BuildChildItems(this)).ToArray(typeof(string)); fdiag.Filter = String.Join("|", fileFilters); for (int i = 0; i < fileFilters.Length; ++i) { if (fileFilters[i].IndexOf(Path.GetExtension(content.ContentName == null ? content.UntitledName : content.ContentName)) >= 0) { fdiag.FilterIndex = i + 1; break; } } if (fdiag.ShowDialog() == DialogResult.OK) { string fileName = fdiag.FileName; IFileService fileService = (IFileService)NetFocus.DataStructure.Services.ServiceManager.Services.GetService(typeof(IFileService)); FileUtilityService fileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService)); if (!fileUtilityService.IsValidFileName(fileName)) { MessageBox.Show("File name " + fileName + " is invalid"); return; } if (fileUtilityService.ObservedSave(new NamedFileOperationDelegate(content.SaveFile), fileName) == FileOperationResult.OK) { fileService.RecentOpenMemeto.AddLastFile(fileName); //MessageBox.Show(fileName, "文件已保存", MessageBoxButtons.OK); } } } } }
public void StoreMemento(IViewContent content) { if (content.ContentName == null) { return; } PropertyService propertyService = (PropertyService)ServiceManager.Services.GetService(typeof(PropertyService)); string directory = propertyService.ConfigDirectory + "temp"; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } XmlDocument doc = new XmlDocument(); doc.LoadXml("<?xml version=\"1.0\"?>\n<Mementoable/>"); XmlAttribute fileAttribute = doc.CreateAttribute("file"); fileAttribute.InnerText = content.ContentName; doc.DocumentElement.Attributes.Append(fileAttribute); IXmlConvertable memento = ((IMementoCapable)content).CreateMemento(); doc.DocumentElement.AppendChild(memento.ToXmlElement(doc)); string fileName = content.ContentName.Substring(3).Replace('/', '.').Replace('\\', '.').Replace(Path.DirectorySeparatorChar, '.'); FileUtilityService fileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService)); // check the file name length because it could be more than the maximum length of a file name string fullFileName = directory + Path.DirectorySeparatorChar + fileName; if (fileUtilityService.IsValidFileName(fullFileName)) { fileUtilityService.ObservedSave(new NamedFileOperationDelegate(doc.Save), fullFileName, FileErrorPolicy.ProvideAlternative); } }