获取特定文件属性
我有一个简单的 WCF 服务,允许客户/消费者向其上传图像、音频或视频文件。上传后,服务应该分析文件并以某种方式检索以下属性:
图像:宽度、高度、拍摄日期、使用的程序
音频:运行时间、艺术家、专辑、流派、比特率、出版年份
视频:运行时间、宽度、高度、帧/秒、视频比特率、音频比特率
显然 Windows 可以很容易地获取和显示这些属性,但我如何在 C# 中做到这一点?
I've got a simple WCF service that lets clients/consumers upload image, audio or video files to it. After the upload, the service is supposed to analyze the file and somehow retrieve the following attributes:
Image: width, height, date taken, program used
Audio: runtime, artist, album, genre, bitrate, publication year
Video: runtime, width, height, frames/sec, video bitrate, audio bitrate
Apparently Windows can get and display these attributes pretty easily, but how do I do it in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由此线程提供。
我已经验证这会获取所有文件属性,包括扩展属性。
在您的项目中,转到“添加引用”->通讯-> “Microsoft Shell 控制和自动化”
再次感谢所述线程,添加一个读取目录中文件属性的 C# 方法。 (我仍在研究是否可以在特定文件上执行此功能。如果不能,您始终可以传递有问题的文件名并验证以仅获取该文件的属性。)
Courtesty of this thread.
I've verified this gets all file attributes including the extended attributes.
In your project go to 'Add Reference' -> COM -> 'Microsoft Shell Controls and Automation'
Add that, and again courtesy of said thread, a C# method to read the attributes of the files in a directory. (I'm still researching to see if it's possible to perform this functionality on a specific file. If not you could always pass the filename in question and verify to only get the attributes out for that file.)
访问该信息的最简单方法是让(Explorer)Shell 为您执行此操作并只需询问(通过 Windows 属性系统)。从 C# 执行此操作的最简单方法可能是使用 适用于 .NET 的 Windows API 代码包。
具体来说,您需要访问 Property Store。如需入门帮助,请查看 PropertiesEditDemo 项目的 Samples 文件夹。
您可以通过读取文件的所有元数据来自己完成此操作,但问题是您的程序必须知道所有可用文件类型的所有可用元数据。我通常更喜欢通过 Shell 来获取这些知识。
The simplest way to access that information is to let the (Explorer) Shell do it for you and just ask (via Windows Property System) for it. And the simplest way to do that from C# is probably to use the Windows API Code Pack for .NET.
Specifically you're going to want to gain access to the Property Store. For help getting started, look in the Samples folder at the PropertiesEditDemo project.
You can do it yourself by reading all the metadata for the file, but the problem is then your program has to know all the available metadata for all the available file types. I generally prefer to hang on the Shell for that knowledge.