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 ReadComment(IStorage storage, string name) { if (storage != null) { using (var comStorage = new ComStorage(storage, false)) { var stream = comStorage.OpenStream(name); using (var comStream = new ComStream(stream, false, false)) { var ser = new XmlSerializer(typeof(Comment)); var wm = ser.Deserialize(comStream) as Comment; Console.WriteLine($"Text: {wm.Text}"); Console.WriteLine($"Author: {wm.Author}"); Console.WriteLine($"Date: {wm.Date}"); } } } else { Console.WriteLine("Comments not available"); } }