以编程方式添加自定义文件属性

时间:2019-01-16 06:52:36

标签: c# vb.net

我需要以编程方式将自定义文件属性(see here)添加到数千个文件中。
WindowsAPICodePack 可以获取/设置现有文件属性,但似乎无法添加自定义属性?!


以下是基于Add new metadata properties to a file的代码:

您必须引用 DSOFile.dll ,可以从Microsoft此处下载:
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 个答案:

没有答案