Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Structured Storage Property Sets


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 date—these 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 designer’s 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].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.