51

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.

4
  • 1
    I suspect for simple files they're in an alternate NTFS stream, e.g. this one but for .docs they'd actually be a property of the doc file itself. I think taglib-sharp is just for mp3 files. There's probably a COM object that's used by Explorer to parse these that you can automate? Commented Mar 17, 2011 at 10:59
  • I cheked out tablib and it is for mp3 only. Maybe give me a clue about COm object, because I dont really understand. I'm a beginner and seem like I'm trying to get something thats over my head. Commented Mar 17, 2011 at 11:06
  • @andree - Thanks for sharing your answer. I was working on similar task and did found that using Shell32.dll I can get properties but was not able to set them. Your answer really helped me. Commented Jul 14, 2015 at 12:15
  • Does this answer your question? Read/Write 'Extended' file properties (C#) Commented May 24, 2020 at 18:33

4 Answers 4

75

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

Sign up to request clarification or add additional context in comments.

9 Comments

Need more +1's for this answer. Can't be having it hanging around at the bottom. Not only is it the best answer for the OP, but also WindowsAPICodePack is just awesome.
Can I add my custom properties?
For some reason, ShellPropertyWriter seems to not save the changes occasionally. Changing the value directly as shown in this answer seems to work better for me.
Can you suggest a .net core solution?
This works with .NET Core now. See stackoverflow.com/questions/65271816/…
|
25

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(); 

10 Comments

Also 1 more possible problem - when trying to write docs which are created with 64-bit office.
I have office 64-bit and it works great. I haven't experienced any problems as of yet. If you don't mind, which Office version are ya running? I've got Home & Student
Does that work for MS Word files only? MS Office files only? What about PDF files which you mentioned in your question?
If I remember correctly - this is only for Office files. For PDFs there are other libraries, which provide this functionality.
@johnywhy It's now the accepted and top rated answer here: stackoverflow.com/a/37987288/3546415 ("MA-Maddin" changed his name to "Martin Schneider")
|
8

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).

1 Comment

But I'd be very surprised if it understood the file formats itself - I'd expect there'd be something under HKCU for the file extension or file data it references that names a COM object that implements a common interface and does the parsing, so that third-parties can write their own and extend the set of file types supported. (But I can't find this.) But yes, may be easier to just get tools to modify PDFs and DOCs.
2

To set properties, you could utilize Windows' Property System. It provides an interface for accessing the "Property Store Cache" (IPropertyStore) where you can read/set any file's properties (regardless of the format), and add your own custom properties (the c library propkey.h has a comprehensive list of all available properties; you can also find these using prop.exe). This is essentially creating a Property Handler that must be later registered to your file extension. It is officially unsupported in managed code, so you might either want to write your own wrapper or use c++ (since this is a c# tagged question).

If you're specifically asking for media properties, check out metadata handlers, which are essentially codecs that extract your properties from the file and also called by explorer by default if you register them correctly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.