private void AccessStreamFromPath(ComStorage storage, string path, bool writable, bool createIfNotExist, Action <ComStream> action, STGM mode = STGM.STGM_SHARE_EXCLUSIVE) { var parentIndex = path.IndexOf('\\'); if (parentIndex == -1) { IStream stream = null; try { stream = storage.OpenStream(path, mode); } catch { if (createIfNotExist) { stream = storage.CreateStream(path); } else { throw; } } using (var comStream = new ComStream(stream, writable)) { action.Invoke(comStream); } } else { var subStorageName = path.Substring(0, parentIndex); IStorage subStorage; try { subStorage = storage.OpenStorage(subStorageName, mode); } catch { if (createIfNotExist) { subStorage = storage.CreateStorage(subStorageName); } else { throw; } } using (var subComStorage = new ComStorage(subStorage, false)) { var nextLevelPath = path.Substring(parentIndex + 1); AccessStreamFromPath(subComStorage, nextLevelPath, writable, createIfNotExist, action); } } }
private static void AddComment(IStorage storage, string text) { var wm = new Comment() { Text = text, Author = Environment.UserName, Date = DateTime.Now }; using (var comStorage = new ComStorage(storage, true)) { var stream = comStorage.CreateStream($"Comment{comStorage.EnumElements().Count() + 1}"); using (var comStream = new ComStream(stream, true)) { var ser = new XmlSerializer(wm.GetType()); ser.Serialize(comStream, wm); } } Console.WriteLine("Comment is added"); }