Add new metadata properties to a file
Asked Answered
S

1

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?

Stannite answered 13/11, 2013 at 7:30 Comment(1)
Depends on the file (the filetype) and the filesystemFaruq
F
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

Faruq answered 14/11, 2013 at 8:19 Comment(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 useFaruq
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?Stannite
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).Faruq
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?Eubanks
Did you install and register the DLL with regsrv32/regasm? There could be a problem. Look here, hope it helps: #18843508Faruq
If you check the source code, DSOFile also uses ADS as well. First, it tries OLE Storage, then it tries explicit metadata handlers registered by file type, and finally it goes on and finally, if that fails it uses alternate data streams (only available on NTFS filesystems). If you were to move the file to a non-NTFS filesystem it would remove the data created in an ADS.Eryn
Your links to sample projects aren't working. Can you please update them? or share a copy if you have ?Computation
@Computation Sorry, there was a routing issue. The links should be working again.Faruq

© 2022 - 2024 — McMap. All rights reserved.