Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set extended file properties?

Tags:

c#

I need to set the Company field value for some Word/PDF documents. I am talking about the extended file properties (summary/author/title, etc.) you see under File Properties.

I know how to get them (by using shell32.dll class library). I assumed that I could also set them with the same class library, but it seems like writing extended properties is a little bit more difficult and shell32.dll doesn't allow that.

I found something about taglib-sharp, which seems to have an option to set extended properties, but I don't really understand how it works.

like image 206
andree Avatar asked Mar 17 '11 10:03

andree


People also ask

How do I change the properties of a file?

Click the File tab. Click Info to view the document properties. To add or change properties, hover your pointer over the property you want to update and enter the information. Note that for some metadata, such as Author, you'll have to right-click on the property and choose Remove or Edit.

How do you add properties to Windows file?

Add or Modify Properties In the desktop, click or tap the File Explorer button on the taskbar. Click or tap the file you want to add or modify properties. In the Details pane, click or tap the tag you want to change, and then type the new tag.

Where do I find the properties of my file?

To view information about a file or folder, right-click it and select Properties. You can also select the file and press Alt + Enter . The file properties window shows you information like the type of file, the size of the file, and when you last modified it.

What is the properties of a file?

When dealing with files, file properties are pieces of information about that file, which can be accessed via a menu item (often called "Properties"). For example, in Microsoft Windows, you can access the properties of a file by right-clicking the file name and selecting Properties.


3 Answers

Add following NuGet packages to your project:

  • Microsoft.WindowsAPICodePack-Shell by Microsoft
  • Microsoft.WindowsAPICodePack-Core by Microsoft

Read and Write Properties

using Microsoft.WindowsAPICodePack.Shell; using Microsoft.WindowsAPICodePack.Shell.PropertySystem;  string filePath = @"C:\temp\example.docx"; var file = ShellFile.FromFilePath(filePath);  // Read and Write:  string[] oldAuthors = file.Properties.System.Author.Value; string oldTitle = file.Properties.System.Title.Value;  file.Properties.System.Author.Value = new string[] { "Author #1", "Author #2" }; file.Properties.System.Title.Value = "Example Title";  // Alternate way to Write:  ShellPropertyWriter propertyWriter =  file.Properties.GetPropertyWriter(); propertyWriter.WriteProperty(SystemProperties.System.Author, new string[] { "Author" }); propertyWriter.Close(); 

Important:

The file must be a valid one, created by the specific assigned software. Every file type has specific extended file properties and not all of them are writable.

If you right-click a file on desktop and cannot edit a property, you wont be able to edit it in code too.

Example:

  • Create txt file on desktop, rename its extension to docx. You can't edit its Author or Title property.
  • Open it with Word, edit and save it. Now you can.

So just make sure to use some try catch

Further Topic: MS Docs: Implementing Property Handlers

like image 188
Martin Schneider Avatar answered Sep 19 '22 20:09

Martin Schneider


Ok here is answer to my own question, since I wasn't really able to find my answer in this forum, it could be useful for others. Solution is to use dsofile.dll and OleDocumentPropertiesClass. Here is MS article about dsofile.dll - Link In this link, you can download dsofile.dll with some other files. But most probably, just like I did, you will face some weird problems that are hard to find a solution for.

1) After intalling dsofile.dll, you will need to register the class: oped cmd and navigate to c:\dsofile of to directory, where you have extracted your downloaded dsofile.dll. After that - write line regsvr32 dsofile.dll. You should get a messagebox saying that registeration was succesful. If not, most propably you don't have admin rights. You are going to need admin rights in case you want this to work.

2) After trying to use this class in your program, if you are using .NET 4.0 it is possible, that you will see error saying something like "class cannot be embedded ..." Well, for that, right click on dsofile in references list, properties -> embed interop files -> set to FALSE.

3) How to use:

    //creates new class of oledocumentproperties     var doc = new OleDocumentPropertiesClass();      //open your selected file     doc.Open(pathToFile, false, dsoFileOpenOptions.dsoOptionDefault);      //you can set properties with summaryproperties.nameOfProperty = value; for example     doc.SummaryProperties.Company = "lol";     doc.SummaryProperties.Author = "me";      //after making changes, you need to use this line to save them     doc.Save(); 
like image 39
andree Avatar answered Sep 22 '22 20:09

andree


Windows Explorer (using shell32.dll) is able to display the extended properties because it understands a lot of different file formats and can parse these. However, to set an extended property you probably need a file format specific library. E.g. to set the author of an MP3 file file is very different compared to setting the author of an Office document. (Actually Windows Explorer allows you to set some extended properties on Office documents.)

The taglib-sharp only works with media files and is most likely not able to set extended properties of any other type of file.

What you need is a library or a tool you can automate to modify PDF files. You can try to google pdf sdk. If you also need to work with Word files you can use COM automation to automate Word. Depending on the Word file format used you may also be able to work directly with the file without having Word installed (XML being much easier than the old binary "streams" format).

like image 36
Martin Liversage Avatar answered Sep 19 '22 20:09

Martin Liversage