/// <summary> /// Opens the compound file storage. /// </summary> /// <param name="pwcsName">The path to the compound file to create</param> /// <param name="ppstg">(out) The IStorage representing this compound file</param> /// <returns>True indicates it opened successfully. False indicates that the file /// could not be found.</returns> public bool OpenStorage( string pwcsName, STGM flags, out IStorage ppstg) { if (!ValidateCompoundFileStorage(pwcsName)) { ppstg = null; return(false); } Ole32Storage.STGOPTIONS stgoptions = new Ole32Storage.STGOPTIONS(); stgoptions.usVersion = 1; stgoptions.ulSectorSize = 4096; Guid guidIStorage = typeof(IStorage).GUID; int result = Ole32Storage.StgOpenStorageEx( pwcsName, flags, STGFMT.STGFMT_DOCFILE, 0, ref stgoptions, IntPtr.Zero, ref guidIStorage, out ppstg); if (result != HRESULT.S_OK) { Marshal.ThrowExceptionForHR(result); } return(true); }
/// <summary> /// Creates the compound file storage /// </summary> /// <param name="pwcsName">The path to the compound file to create</param> /// <param name="ppstg">(out) The IStorage representing this compound file</param> public void CreateStorage( string pwcsName, out IStorage ppstg) { Ole32Storage.STGOPTIONS stgoptions = new Ole32Storage.STGOPTIONS(); stgoptions.usVersion = 1; stgoptions.ulSectorSize = 4096; Guid guidIStorage = typeof(IStorage).GUID; int result = Ole32Storage.StgCreateStorageEx( pwcsName, STGM.FAILIFTHERE | STGM.TRANSACTED | STGM.READWRITE | STGM.SHARE_DENY_WRITE, STGFMT.STGFMT_DOCFILE, 0, ref stgoptions, IntPtr.Zero, ref guidIStorage, out ppstg); if (result != HRESULT.S_OK) { Marshal.ThrowExceptionForHR(result); } }
/// <summary> /// Writes the clsid to this storage. /// </summary> /// <param name="guid">The guid representing the clsid</param> private void WriteClsid(Guid guid) { try { Ole32Storage.WriteClassStg(storage, ref guid); } catch (COMException e) { ThrowStorageException(e); } }
/// <summary> /// Reads the clsid from this storage /// </summary> /// <returns>The guid representing the clsid for this storage</returns> private Guid ReadClsid() { Guid clsid = Guid.NewGuid(); try { Ole32Storage.ReadClassStg(storage, ref clsid); } catch (COMException e) { ThrowStorageException(e); } return(clsid); }
/// <summary> /// Validates that the path to the file is a valid Compound File. /// Returns false if the file does not exist. /// </summary> /// <param name="path"></param> private bool ValidateCompoundFileStorage(string storageName) { if (!File.Exists(storageName)) { //throw new COMException("StorageName does not exist", STG_E.FILENOTFOUND ) ; return(false); } else { int result = Ole32Storage.StgIsStorageFile(storageName); if (result == HRESULT.S_FALSE) { Marshal.ThrowExceptionForHR(STG_E.INVALIDHEADER); } else if (result != HRESULT.S_OK) { Marshal.ThrowExceptionForHR(result); } } return(true); }