示例#1
0
        private void SaveNote(INote note, string localFolder, bool doRoundtrip)
        {
            lock (_lockSaveNote)
            {
                var path     = Path.Combine(localFolder, note.UniqueName + ".xml");
                var tempPath = Path.GetTempFileName();

                var doc = SerializeNote(note);

                using (var file = File.OpenWrite(tempPath)) doc.Save(file);

                try
                {
                    if (doRoundtrip)
                    {
                        var roundtrip = LoadNoteFromFile(tempPath);

                        if (roundtrip.Text != note.Text)
                        {
                            throw new Exception("a.Text != b.Text");
                        }
                        if (roundtrip.Title != note.Title)
                        {
                            throw new Exception("a.Title != b.Title");
                        }
                        if (roundtrip.IsPinned != note.IsPinned)
                        {
                            throw new Exception("a.IsPinned != b.IsPinned");
                        }
                        if (roundtrip.IsLocked != note.IsLocked)
                        {
                            throw new Exception("a.IsLocked != b.IsLocked");
                        }
                        if (!roundtrip.Path.Equals(note.Path))
                        {
                            throw new Exception("a.Path != b.Path");
                        }
                    }

                    File.Copy(tempPath, path, true);
                    note.ResetLocalDirty("Reset local dirty after SaveNote()");
                    File.Delete(tempPath);
                }
                catch (Exception e)
                {
                    throw new Exception("Serialization failed (Sanity check):" + e.Message, e);
                }
            }
        }