22

I want to add some metadata properties to some files. Just like there are Owner, Computer, Title, Subject, etc for doc files, I want to be able to add some custom attributes. How can that be done?

1
  • 2
    Depends on the file (the filetype) and the filesystem
    – jAC
    Commented Nov 14, 2013 at 7:17

1 Answer 1

20

As already mentioned it depends on the filesystem. So this will only work with NTFS.

One way is creating ADS streams: See the edit history.

Another way is using the DSOFile-Library, which is intended to work on Office files only. But it works on every file.

First of all download the library here (x64+x86): DOWNLOAD

IMPORTANT: Since DSO OLE is 32bit DLL it will only work, when you set your compilation target CPU to x86. Otherwise it will throw an Exception. There's also a 64bit version avaliable: How to read custom file properties in c#

Then create a refernce to the COM DLL in your project (Right click on the solution -> Add reference -> COM tab -> Add "DSO OLE Document Property Reader v2.1") and use the namespace:

using DSOFile;

After that you can create your own attributes:

First of all open the file:

OleDocumentProperties myFile = new DSOFile.OleDocumentProperties();
myFile.Open(@"MYPATHHERE", false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);

Create a object for yourValue: object yourValue = "Your Value";

Then check if there's already a property like the one you want to create:

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.set_Value(yourValue);
   }
}

Then after checking for existing attributes, you can add the attribute:

myFile.CustomProperties.Add("Your Property Name", ref yourValue);

To finish the task, save and close the file:

myFile.Save();
myFile.Close(true);

You can download a sample project on my homepage.

Now to the part of showing the attributes in the explorer.

You have to create a shell extension for that. For more info on that, visit the Codeproject page.

I created one, you can download it here. But you have to sign it again (look for a "how-to" on the mentioned page).

It will look like that, when right-clicking on a .css/.js/.txt-file: Shell extension with Sharpshell
Or create your own properties tab:
CustomPropertiesTab
You can download the sample here: DOWNLOAD

For more information about Dsofile.dll and other source see Microsoft Dsofile.dll

8
  • You can download the dsofiles here: abouchleih.com/wp-content/uploads/dsofile.zip The SharpShell dll can be downloaded here: abouchleih.com/wp-content/uploads/SharpShell-Project.zip These are the versions I used and use
    – jAC
    Commented Nov 18, 2013 at 13:25
  • You mentioned in the first version of your comment that the file properties can be added using ADS, if properties were added like this, would they be recognized by windows explorer and displayed in the details category? Commented Nov 26, 2013 at 9:27
  • The metadata stored in the file is only displayed either because you installed a programme which registered a shell extension (e.g. M$ Office, ..) or is already part of the Windows Explorer (> shows metadata of .exe files e.g. version because .exe is M$ specific file format).
    – jAC
    Commented Nov 26, 2013 at 18:55
  • I know that this is an old thread, but anyway. It works in "normal" situations but when I try to access the OleDocumentProperties class from the extension (e.g. overlay extension from SharpShell) I receive this error: Retrieving the COM class factory for component failed. Class not registered. How could you explain this?
    – pangular
    Commented Feb 18, 2015 at 2:08
  • 1
    @mrid Sorry, there was a routing issue. The links should be working again.
    – jAC
    Commented Apr 21, 2020 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.