public void SaveState(bool commit = true) { // First save state to a temp directory, in case there // are any problems saving. If the save works, then replace // the contents of the data dir with those from the temp dir. Console.WriteLine("Saving..."); string dataPath = ".\\data"; string tempPath = ".\\data\\temp"; XmlSerializer x = new XmlSerializer(_cModel.GetType()); Directory.CreateDirectory(dataPath); if (Directory.Exists(tempPath)) { Directory.Delete(tempPath, true); } Directory.CreateDirectory(tempPath); string path = tempPath + "\\canvasstate.xml"; using (XmlWriter xWriter = XmlWriter.Create(path)) { x.Serialize(xWriter, _cModel); } foreach (CCanvas c in _cModel.allCanvases) { c.SaveToFile(tempPath); } x = new XmlSerializer(_wsModel.GetType()); path = tempPath + "\\workspacestate.xml"; using (XmlWriter xWriter = XmlWriter.Create(path)) { x.Serialize(xWriter, _wsModel); } foreach (Workspace ws in _wsModel.allWorkspaces) { ws.SaveNotesToFile(tempPath); } // If the temp save completes, copy the temp files // to the data directory if (commit) //do this only for commit=true (so not autosaves) { foreach (string f in Directory.GetFiles(dataPath)) { File.Delete(f); } foreach (string f in Directory.GetFiles(tempPath)) { File.Copy(string.Format(f), string.Format("{0}\\{1}", dataPath, Path.GetFileName(f))); } } lastSave = DateTime.Now; Console.WriteLine("Saving complete!"); }
protected virtual void OnWorkspaceAdded(WorkspaceModel obj) { var handler = WorkspaceAdded; if (handler != null) handler(obj); WorkspaceEvents.OnWorkspaceAdded(obj.Guid, obj.Name, obj.GetType()); }