First, you need to download the DSOfile from Microsoft. http://support.microsoft.com/kb/224351
Second, you’ll need to reference it in your project
using DSOFile;
Next, you can start looping through the Office predefined properties of each file. Note, some attributes are read-only.
OleDocumentProperties file = new OleDocumentProperties(); file.Open(@"C:myfile.docx", false, dsoFileOpenOptions.dsoOptionDefault); int charCount = file.SummaryProperties.CharacterCount; int wordCount = file.SummaryProperties.WordCount; int pageCount = file.SummaryProperties.PageCount; file.SummaryProperties.Author = "John Smith"; file.SummaryProperties.Category = "My Category"; file.SummaryProperties.Company = "My Company Inc."; file.SummaryProperties.Manager = "David Smith"; file.SummaryProperties.Subject = "Sample files"; file.SummaryProperties.Title = "A very sample file"; file.Save(); file.Close(true);
Those might sometimes not be enough, so surely enough you can add your own custom attributes, and use the NTFS File System as your own personal database.
OleDocumentProperties file = new DSOFile.OleDocumentProperties();
file.Open(@"C:myfile.docx", false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);
string key = "My Custom Key"; /* Use any key you want, these will be saved in the file. */
object value = "My Custom Value";
// Check if file has a certain property set
bool hasProperty = false;
foreach (DSOFile.CustomProperty p in file.CustomProperties)
if (p.Name == key)
hasProperty = true;
// If it doesn't have the property, add it, otherwise set it.
// This is the only way I found to loop through the properties
if (!hasProperty)
file.CustomProperties.Add(key, ref value);
else
foreach (DSOFile.CustomProperty p in file.CustomProperties)
if (p.Name == key)
p.set_Value(value);
// Go through existing custom properties.
foreach (DSOFile.CustomProperty p in file.CustomProperties)
{
Console.WriteLine("{0}:{1}", p.Name, p.get_Value().ToString());
}
file.Save();
file.Close(true);
Quick Links
Legal Stuff