110

我试图找出如何读取/写入 C# 中的扩展文件属性,例如您可以在 Windows 资源管理器中看到的评论、比特率、访问日期、类别等。任何想法如何做到这一点?编辑:我将主要读/写视频文件(AVI/DIVX/...)

4

10 回答 10

89

对于那些不喜欢 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)}");
        }
    }
}
于 2010-01-19T19:16:18.817 回答
32

解决方案 2016

将以下 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();

重要的:

该文件必须是由特定分配的软件创建的有效文件。每种文件类型都有特定的扩展文件属性,并非所有文件都是可写的。

如果您右键单击桌面上的文件并且无法编辑属性,那么您也无法在代码中对其进行编辑。

例子:

  • 在桌面上创建txt文件,将其扩展名重命名为docx。您无法编辑其AuthorTitle属性。
  • 用Word打开,编辑保存。现在你可以。

所以只要确保使用一些try catch

更多主题: Microsoft Docs:实现属性处理程序

于 2016-06-23T09:02:45.800 回答
27

有一个 ID3 阅读器的 CodeProject 文章。还有一个在 kixtart.org上的线程,其中包含有关其他属性的更多信息。基本上,您需要在文件夹外壳对象上GetDetailsOf()调用.shell32.dll

于 2008-10-20T22:17:24.910 回答
25

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

您必须从“引用”对话框的COM选项卡中添加对Microsoft Shell 控件和自动化的引用。

于 2008-11-28T12:28:23.303 回答
8

谢谢你们这个话题!当我想弄清楚 exe 的文件版本时,它帮助了我。但是,我需要自己弄清楚所谓的扩展属性的最后一点。

如果您在 Windows 资源管理器中打开 exe(或 dll)文件的属性,您将获得一个版本选项卡,以及该文件的扩展属性视图。我想访问其中一个值。

对此的解决方案是属性索引器 FolderItem.ExtendedProperty,如果您删除属性名称中的所有空格,您将获得该值。例如,文件版本转到 FileVersion,您就拥有了。

希望这对其他人有帮助,只是想我会将此信息添加到此线程中。干杯!

于 2010-08-23T12:25:28.130 回答
8

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);
 }
于 2013-02-27T12:01:48.937 回答
6

Jerker 的回答要简单一些。这是适用于 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.
}
于 2017-10-09T13:49:13.777 回答
6
  • 在查看了该线程和其他地方的许多解决方案之后,将以下代码放在一起。这只是为了读取一个属性。
  • 我无法让 Shell32.FolderItem2.ExtendedProperty 函数工作,它应该接受一个字符串值并为该属性返回正确的值和类型......这对我来说总是空的,开发人员参考资源非常薄。
  • WindowsApiCodePack似乎已被微软抛弃,它为我们带来了下面的代码。

采用:

string propertyValue = GetExtendedFileProperty("c:\\temp\\FileNameYouWant.ext","PropertyYouWant");
  1. 将返回您想要的扩展属性的值作为给定文件和属性名称的字符串。
  2. 仅循环直到找到指定的属性 - 直到所有属性都像某些示例代码一样被发现
  3. 将在 Windows server 2008 等 Windows 版本上工作,如果只是尝试正常创建 Shell32 对象,您将收到错误“无法将类型为 'System.__ComObject' 的 COM 对象转换为接口类型 '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;
    }
    
于 2018-04-23T01:31:55.363 回答
1

我不确定您要为哪些类型的文件编写属性,但taglib-sharp是一个优秀的开源标记库,它很好地封装了所有这些功能。它为大多数流行的媒体文件类型提供了大量内置支持,但还允许您对几乎任何文件进行更高级的标记。

编辑: 我已经更新了 taglib sharp 的链接。旧链接不再有效。

编辑:根据 kzu 的评论再次更新了链接。

于 2008-10-20T22:42:05.250 回答
1

这是基于我在此页面上找到的内容以及shell32 对象的帮助的读取而不是写入扩展属性的解决方案。

需要明确的是,这是一个 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 相关异常,您将在此处找到解决方案:

使用 Shell32 获取文件扩展属性时出现异常

我不知道这些属性名称在外部系统上会是什么样子,也找不到有关使用哪些可本地化常量来访问字典的信息。我还发现,并非“属性”对话框中的所有属性都出现在返回的字典中。

顺便说一句,这非常慢,并且 - 至少在 Windows 10 上 - 解析检索到的字符串中的日期将是一个挑战,因此使用这似乎是一个坏主意。

在 Windows 10 上,您绝对应该使用包含 SystemPhotoProperties、SystemMusicProperties 等的 Windows.Storage 库。 https://docs.microsoft.com/en-us/windows/uwp/files/quickstart-getting-file-properties

最后,我在那里发布一个使用 WindowsAPICodePack 的更好的解决方案

于 2019-08-10T19:56:29.043 回答