【发布时间】:2010-09-18 05:15:03
【问题描述】:
我正在尝试找出如何在 C# 中读取/写入扩展文件属性 例如您可以在 Windows 资源管理器中看到的评论、比特率、访问日期、类别等。 任何想法如何做到这一点? 编辑:我主要是读/写视频文件(AVI/DIVX/...)
【问题讨论】:
-
这个问题显然没有得到回答,因为接受的答案只显示了如何获取扩展属性而不是如何设置它们。
我正在尝试找出如何在 C# 中读取/写入扩展文件属性 例如您可以在 Windows 资源管理器中看到的评论、比特率、访问日期、类别等。 任何想法如何做到这一点? 编辑:我主要是读/写视频文件(AVI/DIVX/...)
【问题讨论】:
将以下 NuGet 包添加到您的项目中:
Microsoft.WindowsAPICodePack-Shell 来自微软Microsoft.WindowsAPICodePack-Core 来自微软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();
重要:
该文件必须是由特定分配的软件创建的有效文件。每种文件类型都有特定的扩展文件属性,并非所有文件都是可写的。
如果您右键单击桌面上的文件并且无法编辑属性,您也无法在代码中对其进行编辑。
例子:
Author 或Title 属性。所以请确保使用一些 try catch
【讨论】:
这是基于我在此页面和help with shell32 objects 上找到的扩展属性的读取而不是写入的解决方案。
需要明确的是,这是一个 hack。看起来此代码仍将在 Windows 10 上运行,但会遇到一些空属性。以前版本的 Windows 应使用:
var i = 0;
while (true)
{
...
if (String.IsNullOrEmpty(header)) break;
...
i++;
在 Windows 10 上,我们假设有大约 320 个属性可供读取,并简单地跳过空条目:
private Dictionary<string, string> GetExtendedProperties(string filePath)
{
var directory = Path.GetDirectoryName(filePath);
var shell = new Shell32.Shell();
var shellFolder = shell.NameSpace(directory);
var fileName = Path.GetFileName(filePath);
var folderitem = shellFolder.ParseName(fileName);
var dictionary = new Dictionary<string, string>();
var i = -1;
while (++i < 320)
{
var header = shellFolder.GetDetailsOf(null, i);
if (String.IsNullOrEmpty(header)) continue;
var value = shellFolder.GetDetailsOf(folderitem, i);
if (!dictionary.ContainsKey(header)) dictionary.Add(header, value);
Console.WriteLine(header +": " + value);
}
Marshal.ReleaseComObject(shell);
Marshal.ReleaseComObject(shellFolder);
return dictionary;
}
如前所述,您需要引用 Com 程序集 Interop.Shell32。
如果您遇到 STA 相关异常,您将在此处找到解决方案:
Exception when using Shell32 to get File extended properties
我不知道这些属性名称在外部系统上会是什么样子,也找不到有关使用哪些可本地化常量来访问字典的信息。我还发现,并非“属性”对话框中的所有属性都出现在返回的字典中。
顺便说一句,这非常慢,并且 - 至少在 Windows 10 上 - 解析检索到的字符串中的日期将是一个挑战,因此开始使用它似乎不是一个好主意。
在 Windows 10 上,您绝对应该使用包含 SystemPhotoProperties、SystemMusicProperties 等的 Windows.Storage 库。 https://docs.microsoft.com/en-us/windows/uwp/files/quickstart-getting-file-properties
最后,我发布了一个更好的解决方案,它使用 WindowsAPICodePack there
【讨论】:
用途:
string propertyValue = GetExtendedFileProperty("c:\\temp\\FileNameYouWant.ext","PropertyYouWant");
适用于 Windows server 2008 等 Windows 版本,如果只是尝试正常创建 Shell32 对象,则会收到错误 "Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.Shell'"。
public static string GetExtendedFileProperty(string filePath, string propertyName)
{
string value = string.Empty;
string baseFolder = Path.GetDirectoryName(filePath);
string fileName = Path.GetFileName(filePath);
//Method to load and execute the Shell object for Windows server 8 environment otherwise you get "Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.Shell'"
Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
Object shell = Activator.CreateInstance(shellAppType);
Shell32.Folder shellFolder = (Shell32.Folder)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { baseFolder });
//Parsename will find the specific file I'm looking for in the Shell32.Folder object
Shell32.FolderItem folderitem = shellFolder.ParseName(fileName);
if (folderitem != null)
{
for (int i = 0; i < short.MaxValue; i++)
{
//Get the property name for property index i
string property = shellFolder.GetDetailsOf(null, i);
//Will be empty when all possible properties has been looped through, break out of loop
if (String.IsNullOrEmpty(property)) break;
//Skip to next property if this is not the specified property
if (property != propertyName) continue;
//Read value of property
value = shellFolder.GetDetailsOf(folderitem, i);
}
}
//returns string.Empty if no value was found for the specified property
return value;
}
【讨论】:
shell 转换为IShellDispatch 的任何接口(1-6)并直接调用成员,而不是使用InvokeMember()。 Shell32.IShellDispatch ishell = (Shell32.IShellDispatch)shell; Shell32.Folder shellFolder = ishell.NameSpace(baseFolder);
VB.NET 中的这个示例读取所有扩展属性:
Sub Main()
Dim arrHeaders(35)
Dim shell As New Shell32.Shell
Dim objFolder As Shell32.Folder
objFolder = shell.NameSpace("C:\tmp")
For i = 0 To 34
arrHeaders(i) = objFolder.GetDetailsOf(objFolder.Items, i)
Next
For Each strFileName In objfolder.Items
For i = 0 To 34
Console.WriteLine(i & vbTab & arrHeaders(i) & ": " & objfolder.GetDetailsOf(strFileName, i))
Next
Next
End Sub
您必须从 References 对话框的 COM 选项卡中添加对 Microsoft Shell 控件和自动化 的引用。
【讨论】:
对于那些不喜欢VB的人,这里是c#:
注意,您必须从“引用”对话框的 COM 选项卡中添加对 Microsoft Shell 控件和自动化的引用。
public static void Main(string[] args)
{
List<string> arrHeaders = new List<string>();
Shell32.Shell shell = new Shell32.Shell();
Shell32.Folder objFolder;
objFolder = shell.NameSpace(@"C:\temp\testprop");
for( int i = 0; i < short.MaxValue; i++ )
{
string header = objFolder.GetDetailsOf(null, i);
if (String.IsNullOrEmpty(header))
break;
arrHeaders.Add(header);
}
foreach(Shell32.FolderItem2 item in objFolder.Items())
{
for (int i = 0; i < arrHeaders.Count; i++)
{
Console.WriteLine(
$"{i}\t{arrHeaders[i]}: {objFolder.GetDetailsOf(item, i)}");
}
}
}
【讨论】:
Jerker's answer 更简单一些。这是工作的示例代码from MS:
var folder = new Shell().NameSpace(folderPath);
foreach (FolderItem2 item in folder.Items())
{
var company = item.ExtendedProperty("Company");
var author = item.ExtendedProperty("Author");
// Etc.
}
对于那些不能静态引用shell32的,你可以像这样动态调用它:
var shellAppType = Type.GetTypeFromProgID("Shell.Application");
dynamic shellApp = Activator.CreateInstance(shellAppType);
var folder = shellApp.NameSpace(folderPath);
foreach (var item in folder.Items())
{
var company = item.ExtendedProperty("Company");
var author = item.ExtendedProperty("Author");
// Etc.
}
【讨论】:
GetDetailsOf() 方法 - 检索有关文件夹中项目的详细信息。例如,它的大小、类型或上次修改的时间。文件属性可能因Windows-OS 版本而异。
List<string> arrHeaders = new List<string>();
Shell shell = new ShellClass();
Folder rFolder = shell.NameSpace(_rootPath);
FolderItem rFiles = rFolder.ParseName(filename);
for (int i = 0; i < short.MaxValue; i++)
{
string value = rFolder.GetDetailsOf(rFiles, i).Trim();
arrHeaders.Add(value);
}
【讨论】:
Folder rFolder = shell.NameSpace(_rootPath); 在运行时遇到 COM 异常。仅供参考,我使用的是 Windows 8 操作系统。
我不确定您要为哪些类型的文件编写属性,但taglib-sharp 是一个出色的开源标记库,它很好地封装了所有这些功能。它为大多数流行的媒体文件类型提供了大量内置支持,还允许您对几乎任何文件进行更高级的标记。
编辑:我已经更新了 taglib sharp 的链接。旧链接不再有效。
编辑:根据 kzu 的评论再次更新了链接。
【讨论】:
谢谢你们这个话题!当我想弄清楚 exe 的文件版本时,它帮助了我。但是,我需要自己弄清楚所谓的扩展属性的最后一点。
如果您在 Windows 资源管理器中打开 exe(或 dll)文件的属性,您将获得一个版本选项卡,以及该文件的扩展属性视图。我想访问其中一个值。
解决方案是属性索引器 FolderItem.ExtendedProperty,如果您删除属性名称中的所有空格,您将获得该值。例如。文件版本转到 FileVersion,你就拥有了。
希望这对其他人有所帮助,只是想我会将此信息添加到此线程中。干杯!
【讨论】:
ID3 读卡器有a CodeProject article。还有一个thread at kixtart.org,其中包含有关其他属性的更多信息。基本上,您需要为shell32.dll 调用文件夹 shell 对象上的GetDetailsOf() method。
【讨论】: