简体   繁体   English

如何设置扩展文件属性?

[英]How to set extended file properties?

I need to set the Company field value for some Word/PDF documents.我需要为某些 Word/PDF 文档设置 Company 字段值。 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).我知道如何获取它们(通过使用shell32.dll类库)。 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.我假设我也可以使用相同的类库设置它们,但似乎编写扩展属性有点困难, shell32.dll不允许这样做。

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.我发现了一些关于taglib-sharp东西,它似乎有一个设置扩展属性的选项,但我真的不明白它是如何工作的。

Add following NuGet packages to your project:将以下 NuGet 包添加到您的项目中:

  • Microsoft.WindowsAPICodePack-Shell by Microsoft Microsoft.WindowsAPICodePack-Shell由 Microsoft
  • Microsoft.WindowsAPICodePack-Core 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.在桌面上创建txt文件,将其扩展名重命名为docx。 You can't edit its Author or Title property.您无法编辑其AuthorTitle属性。
  • Open it with Word, edit and save it.用 Word 打开它,编辑并保存它。 Now you can.现在你可以。

So just make sure to use some try catch所以只要确保使用一些try catch

Further Topic: MS Docs: Implementing Property Handlers更多主题: MS 文档:实现属性处理程序

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.解决方案是使用 dsofile.dll 和 OleDocumentPropertiesClass。 Here is MS article about dsofile.dll - Link In this link, you can download dsofile.dll with some other files.这是关于 dsofile.dll 的 MS 文章 -链接在此链接中,您可以下载 dsofile.dll 和其他一些文件。 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. 1) 安装 dsofile.dll 后,您需要注册类:oped cmd 并导航到目录的 c:\\dsofile,您已在该目录中解压下载的 dsofile.dll。 After that - write line regsvr32 dsofile.dll .之后 - 写入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. 2) 尝试在您的程序中使用此类后,如果您使用的是 .NET 4.0,您可能会看到类似“类无法嵌入...”之类的错误信息,为此,右键单击 dsofile引用列表,属性 -> 嵌入互操作文件 -> 设置为 FALSE。

3) How to use: 3) 使用方法:

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

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. Windows 资源管理器(使用shell32.dll )能够显示扩展属性,因为它理解许多不同的文件格式并且可以解析这些。 However, to set an extended property you probably need a file format specific library.但是,要设置扩展属性,您可能需要一个特定于文件格式的库。 Eg to set the author of an MP3 file file is very different compared to setting the author of an Office document.例如,设置 MP3 文件的作者与设置 Office 文档的作者非常不同。 (Actually Windows Explorer allows you to set some extended properties on Office documents.) (实际上,Windows 资源管理器允许您在 Office 文档上设置一些扩展属性。)

The taglib-sharp only works with media files and is most likely not able to set extended properties of any other type of file. taglib-sharp 仅适用于媒体文件,很可能无法设置任何其他类型文件的扩展属性。

What you need is a library or a tool you can automate to modify PDF files.您需要的是一个可以自动修改 PDF 文件的库或工具。 You can try to google pdf sdk .您可以尝试google pdf sdk If you also need to work with Word files you can use COM automation to automate Word.如果您还需要处理 Word 文件,您可以使用 COM 自动化来自动化 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).根据所使用的 Word 文件格式,您也可以直接使用该文件而无需安装 Word(XML 比旧的二进制“流”格式容易得多)。

To set properties, you could utilize Windows' Property System.要设置属性,您可以使用Windows 的属性系统。 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 ).它提供了一个用于访问“属性存储缓存”(IPropertyStore)的接口,您可以在其中读取/设置任何文件的属性(无论格式如何),并添加您自己的自定义属性(c 库 propkey.h 具有所有可用属性;您也可以使用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 ac# tagged question).它在托管代码中不受官方支持,因此您可能想要编写自己的包装器或使用 c++(因为这是 ac# 标记的问题)。

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.如果您特别要求媒体属性,请查看元数据处理程序,这些处理程序本质上是从文件中提取您的属性的编解码器,如果您正确注册它们,默认情况下也会由资源管理器调用。

Thanks @andree, I have search much for answers.. (For me it worked only with .doc files).谢谢@andree,我搜索了很多答案..(对我来说它只适用于 .doc 文件)。 Added添加

if (!doc.IsOleFile)
    Console.ReadLine();

Just to check if Ole file..只是为了检查 Ole 文件是否..

And Visual Studio as Administrator和 Visual Studio 作为管理员

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM