读/写“扩展” 文件属性 (C#)
我试图找出如何在 C# 中读取/写入扩展文件属性 例如您可以在 Windows 资源管理器中看到的评论、比特率、访问日期、类别等。 有什么想法如何做到这一点? 编辑:我将主要读取/写入视频文件(AVI/DIVX/...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我试图找出如何在 C# 中读取/写入扩展文件属性 例如您可以在 Windows 资源管理器中看到的评论、比特率、访问日期、类别等。 有什么想法如何做到这一点? 编辑:我将主要读取/写入视频文件(AVI/DIVX/...)
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(11)
对于那些不喜欢 VB 的人,这里是 C# 版本:
注意,您必须从“引用”对话框的 COM 选项卡添加对 Microsoft Shell 控件和自动化 的引用。
For those of not crazy about VB, here it is in c#:
Note, you have to add a reference to Microsoft Shell Controls and Automation from the COM tab of the References dialog.
解决方案 2016
将以下 NuGet 包添加到您的项目中:
Microsoft.WindowsAPICodePack-Shell
MicrosoftMicrosoft.WindowsAPICodePack-Core
Microsoft 的读写属性
重要:
该文件必须是有效的,由指定的特定软件创建。 每种文件类型都有特定的扩展文件属性,但并非所有文件类型都是可写的。
如果右键单击桌面上的文件但无法编辑属性,则也无法在代码中编辑它。
示例:
编辑其
Author
或Title
属性。它。 现在你可以。
因此,请确保使用一些
try
catch
进一步主题:
Microsoft Docs:实现属性处理程序
Solution 2016
Add following NuGet packages to your project:
Microsoft.WindowsAPICodePack-Shell
by MicrosoftMicrosoft.WindowsAPICodePack-Core
by MicrosoftRead and Write Properties
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:
edit its
Author
orTitle
property.it. Now you can.
So just make sure to use some
try
catch
Further Topic:
Microsoft Docs: Implementing Property Handlers
有一篇针对 ID3 阅读器的CodeProject 文章。 以及 kixtart.org 上的帖子其中包含其他属性的更多信息。 基本上,您需要调用
GetDetailsOf()<
shell32.dll
的 文件夹 shell 对象上的 /code> 方法。
There's a CodeProject article for an ID3 reader. And a thread at kixtart.org that has more information for other properties. Basically, you need to call the
GetDetailsOf()
method on the folder shell object forshell32.dll
.VB.NET 中的此示例读取所有扩展属性:
您必须从引用的COM选项卡添加对Microsoft Shell Controls and Automation的引用> 对话框。
This sample in VB.NET reads all extended properties:
You have to add a reference to Microsoft Shell Controls and Automation from the COM tab of the References dialog.
谢谢你们这个话题! 当我想找出 exe 文件版本时,它对我很有帮助。 然而,我需要自己弄清楚所谓的扩展属性的最后一点。
如果在 Windows 资源管理器中打开 exe(或 dll)文件的属性,您将看到“版本”选项卡以及该文件的“扩展属性”视图。 我想访问这些值之一。
解决这个问题的方法是使用属性索引器FolderItem.ExtendedProperty,如果删除属性名称中的所有空格,您将获得该值。 例如,文件版本变为 FileVersion,然后就可以了。
希望这对其他人有帮助,只是想将此信息添加到此线程中。 干杯!
Thank you guys for this thread! It helped me when I wanted to figure out an exe's file version. However, I needed to figure out the last bit myself of what is called Extended Properties.
If you open properties of an exe (or dll) file in Windows Explorer, you get a Version tab, and a view of Extended Properties of that file. I wanted to access one of those values.
The solution to this is the property indexer FolderItem.ExtendedProperty and if you drop all spaces in the property's name, you'll get the value. E.g. File Version goes FileVersion, and there you have it.
Hope this helps anyone else, just thought I'd add this info to this thread. Cheers!
GetDetailsOf()
方法 - 检索有关文件夹中项目的详细信息。 例如,其大小、类型或上次修改的时间。 文件属性可能因Windows 操作系统
版本而异。GetDetailsOf()
Method - Retrieves details about an item in a folder. For example, its size, type, or the time of its last modification. File Properties may vary based on theWindows-OS
version.Jerker 的答案稍微简单一些。 以下是可从 MS 运行的示例代码< /a>:
对于那些无法静态引用 shell32 的人,您可以像这样动态调用它:
Jerker's answer is little simpler. Here's sample code which works from MS:
For those who can't reference shell32 statically, you can invoke it dynamically like this:
下面的代码被放在一起。 这只是为了读取一个属性。
Shell32.FolderItem2.ExtendedProperty 函数可以工作,应该是
获取一个字符串值并返回正确的值和类型
属性...这对我来说始终是空的,并且开发人员参考资源非常薄弱。
已被微软放弃,这给我们带来了下面的代码。
使用:
给定文件和属性名称的字符串。
所有属性都像一些示例代码一样被发现
将在 Windows 版本(例如 Windows Server 2008)上工作,您将收到错误 “如果只是尝试正常创建 Shell32 对象,则无法将类型为“System.__ComObject”的 COM 对象转换为接口类型“Shell32.Shell””。 p>
the following code was put together. This is only to read a property.
Shell32.FolderItem2.ExtendedProperty function to work, it is supposed
to take a string value and return the correct value and type for that
property... this was always null for me and developer reference resources were very thin.
to have been abandoned by Microsoft which brings us the code below.
Use:
string for the given file and property name.
all properties are discovered like some sample code
Will work on Windows versions like Windows server 2008 where you will get the error "Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.Shell'" if just trying to create the Shell32 Object normally.
这是一个用于读取(而不是写入)扩展属性的解决方案,基于我在此页面和 有关 shell32 对象的帮助。
需要明确的是,这是一个黑客行为。 看起来此代码仍将在 Windows 10 上运行,但会遇到一些空属性。 以前版本的 Windows 应该使用:
在 Windows 10 上,我们假设有大约 320 个属性需要读取,并简单地跳过空条目:
如前所述,您需要引用 Com 程序集 Interop.Shell32。
如果您遇到 STA 相关异常,您可以在这里找到解决方案:
使用 Shell32 获取文件扩展属性时出现异常
我不知道这些属性名称在外部系统上会是什么样子,也找不到有关使用哪些可本地化常量来访问字典的信息。 我还发现,并非“属性”对话框中的所有属性都出现在返回的字典中。
顺便说一句,这非常慢,而且至少在 Windows 10 上,解析检索到的字符串中的日期将是一个挑战,因此从一开始使用它似乎是一个坏主意。
在 Windows 10 上,您绝对应该使用 Windows.Storage 库,其中包含 SystemPhotoProperties、SystemMusicProperties 等。
https://learn.microsoft.com/ en-us/windows/uwp/files/quickstart-getting-file-properties
最后,我发布了一个使用 WindowsAPICodePack 的更好的解决方案那里
Here is a solution for reading - not writing - the extended properties based on what I found on this page and at help with shell32 objects.
To be clear this is a hack. It looks like this code will still run on Windows 10 but will hit on some empty properties. Previous version of Windows should use:
On Windows 10 we assume that there are about 320 properties to read and simply skip the empty entries:
As mentioned you need to reference the Com assembly Interop.Shell32.
If you get an STA related exception, you will find the solution here:
Exception when using Shell32 to get File extended properties
I have no idea what those properties names would be like on a foreign system and couldn't find information about which localizable constants to use in order to access the dictionary. I also found that not all the properties from the Properties dialog were present in the dictionary returned.
BTW this is terribly slow and - at least on Windows 10 - parsing dates in the string retrieved would be a challenge so using this seems to be a bad idea to start with.
On Windows 10 you should definitely use the Windows.Storage library which contains the SystemPhotoProperties, SystemMusicProperties etc.
https://learn.microsoft.com/en-us/windows/uwp/files/quickstart-getting-file-properties
And finally, I posted a much better solution that uses WindowsAPICodePack there
我不确定您要尝试为其写入属性的文件类型是 taglib-sharp 是一个优秀的开源标记库,它很好地封装了所有这些功能。 它对大多数流行的媒体文件类型有很多内置支持,但也允许您对几乎任何文件进行更高级的标记。
编辑:我已经更新了 taglib Sharp 的链接。 旧链接不再有效。
编辑:根据 kzu 的评论再次更新了链接。
I'm not sure what types of files you are trying to write the properties for but taglib-sharp is an excellent open source tagging library that wraps up all this functionality nicely. It has a lot of built in support for most of the popular media file types but also allows you to do more advanced tagging with pretty much any file.
EDIT: I've updated the link to taglib sharp. The old link no longer worked.
EDIT: Updated the link once again per kzu's comment.
StorageFile 类 可以读取和写入文件的扩展属性。 StorageFolder 类 可以用于读取文件夹的属性,但大多数文件夹的属性无法更新。 可以使用以下方式访问该文件:
要读取视频文件的
Director
,扩展属性名称为System.Video.Director
; 要读取的属性列表可以指定为:并使用以下方式检索属性:
然后结果位于
extraProperties[PropertyName]
中。 对于诸如Director
之类的属性,对象是字符串数组,但对于其他属性,对象可能是字符串或其他内容。要更新属性,请使用
List>
,如下所示:在我的测试中,当我更新属性然后立即读回它时,我得到了之前的结果,但该属性是后来检查时更新了。
如果您的项目属性的目标操作系统版本为
10.0.17763.0
或更高版本,那么您就可以访问这些类。 该文档将这些类称为 UWP 和 WinRT,但它们在 .Net 内置中可用。The StorageFile Class can read and write extended properties for files. The StorageFolder Class can be used to read properties for folders but most of the properties for folders cannot be updated. The file can be accessed using:
To read the
Director
for a video file the extended property name isSystem.Video.Director
; a list of properties to be read can be specified as in:And the properties retrieved using:
Then the results are in
extraProperties[PropertyName]
. For a property such asDirector
the object is a string array but for other properties the object might be a string or something else.To update a property a
List<KeyValuePair<string, object>>
is used as in:In my testing, when I updated a property then immediately read it back I got the previous results but the property was updated when they were checked later.
If your project's properties have
10.0.17763.0
or greater for the Target OS Version then you have access to the classes. The documentation refers to the classes as being UWP and WinRT but they are available in .Net built-in.