One of the questions that often appears in the .NET newsgroups is: How
do I get the properties for an Office file? This usually does not refer
to properties like the file size and creation datethese properties are
easily obtained through the FileInfo
and File
classes in the System.IO
namespace. Instead, the question refers to the properties obtained through the
Properties menu item in an Office application, or on the Summary page of the
property dialog of a file in Windows Explorer. These properties are obtained
through OLE property sets, and currently there are no classes in .NET to access
them. In this article, I will explain what property sets are and how to access
them.
Years before .NET was even a scribble on its designers notepad, the Windows world was ruled by Object Linking and Embedding (OLE), which allowed you to share data through OLE objects. OLE objects could be persisted to a clipboard format or to a file, and this persisted object could be embedded, or a link to its file could be added, to an OLE document. OLE objects could be composed of several other objects, and since this could lead to multiple levels of nesting in an OLE document, the binary format had to be structured. OLE documents used structured storage to achieve this. Structured storage essentially implements a file system within a file, so there are streams of binary data within containers called storages. A storage can have multiple streams and multiple storages, just as a folder can contain multiple files and multiple subfolders.
Property sets are streams within the root storage of an OLE document, there are two property sets defined for Office documents with the odd names of "\005SummaryInformation" and "\005DocumentSummaryInformation." Note that the name of these streams start with the character that has an ASCII value of 5. There are standard properties defined for these property sets that you can find in propidl.h; most of the useful properties can be found in "\005SummaryInformation" and this is the property set that I have covered in the same code. Because property sets are streams, you have to read the data as a blob and iterate through the bytes reading the blobs for the various properties, and then convert them to appropriate data types. Such code is ugly and difficult to write, so the OLE team came up with OLE interfaces that iterate the properties for you. Instead of using the odd readable names for the property sets, the OLE interfaces use GUIDs and objidl.h has extern definitions for the standard property sets.
The process for reading a property set is quite straightforward: First, you
call ::StgOpenStorageEx()
to open the OLE document as a compound file.
If you request that this function should return an IStorage
interface,
you can use this to get access to the property set by requesting its IStream
.
The simpler action is to request access via an IPropertySetStorage
interface.
This interface has a method called Open()
, to which you pass the GUID
for the property set, and it will return an IPropertyStorage
interface
through which you can access a property and its value. For example, the following code will print the author and document title
for a Word document called test.doc:
SummaryInformation summary = new SummaryInformation("test.doc"); Console.WriteLine("author is " + summary.Author + " title is " + summary.Title);
It would be simple to change this code to give the DocumentSummaryInformation property set, or a custom property set.
Richard Grimes speaks at conferences and writes extensively on .NET, COM, and COM+. He is the author of Developing Applications with Visual Studio .NET (Addison-Wesley, 2002). If you have comments about this topic, Richard can be reached at [email protected].