using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; //using System.IO; //using System.Runtime.InteropServices; namespace Biff8Excel.COM { public sealed class Storage : IDisposable { private bool disposed; private IStorage storage; public Storage(IStorage storage) { this.storage = storage; } ~Storage() { this.Dispose(); } public static Storage CreateDocFile(string storageFile, StorageMode mode) { IStorage storage = NativeMethods.StgCreateDocfile(storageFile, (int)mode, 0); return new Storage(storage); } public static Storage Open(string storageFile, StorageMode mode) { IStorage storage = NativeMethods.StgOpenStorage(storageFile, IntPtr.Zero, (int)mode, IntPtr.Zero, 0); return new Storage(storage); } public void CopyTo(Storage destinationStorage) { this.storage.CopyTo(0, IntPtr.Zero, IntPtr.Zero, destinationStorage.storage); } public Storage OpenStorage(string name, bool autoCreate) { IStorage subStorage; try { this.storage.OpenStorage(name, null, (int)(StorageMode.ReadWrite | StorageMode.ShareExclusive), IntPtr.Zero, 0, out subStorage); } catch (COMException) { subStorage = null; } if (subStorage == null) { if (autoCreate) return CreateStorage(name); return null; } return new Storage(subStorage); } public Storage RecurOpenStorage(string name, bool autoCreate) { string pwcsName; if (name == null) throw new Biff8ExcelException("名称不得为null"); int pos = name.IndexOf('\\'); if (pos > 0) { pwcsName = name.Substring(0, pos); name = name.Substring(pos + 1); } else { pwcsName = name; name = ""; } Storage subStorage = OpenStorage(pwcsName, autoCreate); if (subStorage != null && name.Length > 0) { return subStorage.RecurOpenStorage(name, autoCreate); } return subStorage; } public void Dispose() { if (!this.disposed) { Marshal.ReleaseComObject(this.storage); this.storage = null; this.disposed = true; } GC.SuppressFinalize(this); } public Storage CreateStorage(string name) { IStorage subStorage = null; try { //this.storage.OpenStorage(name, null, // (int)(StorageMode.ReadWrite | StorageMode.ShareExclusive), // IntPtr.Zero, 0, out subStorage); this.storage.CreateStorage(name, (int)(StorageMode.Create | StorageMode.ReadWrite | StorageMode.ShareExclusive), 0, 0, out subStorage); this.storage.Commit(0); return new Storage(subStorage); } catch (COMException) { if (subStorage != null) Marshal.ReleaseComObject(subStorage); } return null; } public COMStream CreateStream(string name) { IStreamRef subStream = null; try { //this.storage.OpenStream(name, IntPtr.Zero, // (int)(StorageMode.ReadWrite | StorageMode.ShareExclusive), // 0, out subStream); //if (subStream != null) // this.storage.DestroyElement(name); //Now create the element this.storage.CreateStream(name, (int)(StorageMode.Create | StorageMode.ReadWrite | StorageMode.ShareExclusive), 0, 0, out subStream); this.storage.Commit(0); return new COMStream(subStream); } catch (COMException) { if (subStream != null) Marshal.ReleaseComObject(subStream); return null; } } public COMStream OpenStream(string name) { IStreamRef subStream; try { this.storage.OpenStream(name, IntPtr.Zero, (int)(StorageMode.ReadWrite | StorageMode.ShareExclusive), 0, out subStream); return new COMStream(subStream); } catch (COMException) { return null; } } public void Commit(int grfCommitFlags) { this.storage.Commit(grfCommitFlags); } } }