Add custom file properties programmatically

Viewed 1494

I need to add custom file properties (see here) to thousands of files programmatically.
The WindowsAPICodePack can get/set existing file properties, but it seems it can not add custom properties?!?


Here the code that works based on Add new metadata properties to a file:

You must reference the DSOFile.dll which can be downloaded from Microsoft here:
Microsoft Developer Support OLE File Property Reader 2.1 Sample

using DSOFile;

OleDocumentProperties myFile = new DSOFile.OleDocumentProperties();
myFile.Open(@"c:\temp\B30700.asm", false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);

bool property_exists;
object prop_value;

prop_value = "999";

//Then check if there's already a property like the one you want to create
property_exists = false;

foreach (DSOFile.CustomProperty property in myFile.CustomProperties)
{
    if (property.Name == "Your Property Name")
    {
        //Property exists
        //End the task here (return;) oder edit the property
        property_exists = true;
        property.set_Value(prop_value);
    }
}

if (!property_exists)
    myFile.CustomProperties.Add("Your Property Name", ref prop_value);

myFile.Save();
myFile.Close(true);
0 Answers
Related