Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

NTFS stores a lot more information than is obviously visible at first, but .Net doesn't yet have support natively for accessing and writing these Custom Attributes, or File Metadata. However, Microsoft released a library called DSOfile, which is intended for changing properties of Office files if you don't have Office installed. It can, however, work on any type of files.

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);