January 28, 2021
Hot Topics:

Accessing Files and Directories

  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Getting File Properties for Office Documents

Microsoft has an ActiveX component that can be used to programmatically retrieve the summary properties (title, subject, etc.) for files such as Excel, Word, and PowerPoint. It has advantages because it does not use Office Automation so Microsoft Office does not have to be installed. This component can be used to produce a listing of files and their properties.

Sample Code to Produce File Listing with Properties

The following code will populate a DataTable with a list of files and their properties. The DataTable could be bound to a DataGrid or another display control as desired. Be sure you've added the appropriate reference to the dsofile.dll that exposes the file properties. Because this is a COM-based DLL, COM Interop will be used to interact with the DLL.

// Setup the data tableDataTable fileTable = new DataTable("Files");DataRow fileRow;fileTable.Columns.Add("Name");fileTable.Columns.Add("Title");fileTable.Columns.Add("Subject");fileTable.Columns.Add("Description");// Open the directoryDirectoryInfo docDir = new DirectoryInfo("C:\\My Documents\\");if( !docDir.Exists ){  docDir.Create();}// Add the document info into the tableDSOleFile.PropertyReader propReader =       new DSOleFile.PropertyReaderClass();DSOleFile.DocumentProperties docProps;foreach(FileInfo file in docDir.GetFiles()){  try  {   fileRow = fileTable.NewRow();   docProps = propReader.GetDocumentProperties(file.FullName);   fileRow["Name"] = file.Name;   fileRow["Title"] = docProps.Title;   fileRow["Subject"] = docProps.Subject;   fileRow["Description"] = docProps.Comments;   fileTable.Rows.Add(fileRow);  }  catch( Exception exception )  {   Console.WriteLine("Error occurred: " + exception.Message);  }}propReader = null;this.DocumentGrid.DataSource = fileTable;this.DocumentGrid.DataBind();// Force cleanup so dsofile doesn't keep files locked openGC.Collect();


You now have seen several cursory ways in which the System.IO namespace can be used to interact with files and directories. We took an additional look to see how to use the dsofile additional DLL from Microsoft to show the properties for Microsoft Office documents.

Future Columns

If you have something in particular that you would like to see explained here, you can reach me at mstrawmyer@crowechizek.com.

About the Author

Mark Strawmyer, MCSD, MCSE (NT4/W2K), MCDBA is a Senior Architect of .NET applications for large- and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in architecture, design, and development of Microsoft-based solutions. You can reach Mark at mstrawmyer@crowechizek.com.

# # #

Page 3 of 3

This article was originally published on September 9, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date