Home

Awesome

Build Status Github workflow

openmcdf

Structured Storage .net component - pure C#

OpenMCDF is a 100% .net / C# component that allows developers to manipulate Microsoft Compound Document Files (also known as OLE structured storage).

Compound file includes multiple streams of information (document summary, user data) in a single container.

This file format is used under the hood by a lot of applications: all the documents created by Microsoft Office until the 2007 product release are structured storage files. Windows thumbnails cache files (thumbs.db) are compound documents as well as .msg Outlook messages. Visual Studio .suo files (solution options) are compound files and a lot of audio/video editing tools save project file in a compound container (*.aaf files for example).

OpenMcdf supports read/write operations on streams and storages and traversal of structures tree. It supports version 3 and 4 of the specifications, uses lazy loading wherever possible to reduce memory usage and offer an intuitive API to work with structured files.

It's very easy to create a new compound file

byte[] b = new byte[10000];

CompoundFile cf = new CompoundFile();
CFStream myStream = cf.RootStorage.AddStream("MyStream");

myStream.SetData(b);
cf.Save("MyCompoundFile.cfs");
cf.Close();

You can open an existing one, an excel workbook (.xls) and use its main data stream

//A xls file should have a Workbook stream
String filename = "report.xls";
CompoundFile cf = new CompoundFile(filename);
CFStream foundStream = cf.RootStorage.GetStream("Workbook");
byte[] temp = foundStream.GetData();
//do something with temp
cf.Close();

Adding storage and stream items is just as easy...

CompoundFile cf = new CompoundFile();
CFStorage st = cf.RootStorage.AddStorage("MyStorage");
CFStream sm = st.AddStream("MyStream");

...as removing them

cf.RootStorage.Delete("AStream"); // AStream item is assumed to exist.

Call commit() method when you need to persist changes to the underlying stream

cf.RootStorage.AddStream("MyStream").SetData(buffer);
cf.Commit();

If you need to compress a compound file, you can purge its unused space

CompoundFile.ShrinkCompoundFile("MultipleStorage_Deleted_Compress.cfs"); 

OLE Properties handling for DocumentSummaryInfo and SummaryInfo streams
is now available via extension methods (experimental - api subjected to changes)

PropertySetStream mgr = ((CFStream)target).AsOLEProperties();
for (int i = 0; i < mgr.PropertySet0.NumProperties; i++)
{
  ITypedPropertyValue p = mgr.PropertySet0.Properties[i];
  ...

OpenMcdf runs happily on the Mono platform and targets netstandard 2.0 to allow maximum client compatibility AND .net framework 4 for legacy implementations.