Read/Write 'Extended' file properties (C#)

后端 未结 10 2205
我在风中等你
我在风中等你 2020-11-22 02:54

I\'m trying to find out how to read/write to the extended file properties in C# e.g. Comment, Bit Rate, Date Accessed, Category etc that you can see in Windows explorer. An

相关标签:
10条回答
  • 2020-11-22 03:46

    Solution 2016

    Add following NuGet packages to your project:

    • Microsoft.WindowsAPICodePack-Shell 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. You can't edit its Author or Title property.
    • Open it with Word, edit and save it. Now you can.

    So just make sure to use some try catch

    Further Topic: MSDN: Implementing Property Handlers

    0 讨论(0)
  • 2020-11-22 03:46

    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.

    0 讨论(0)
  • 2020-11-22 03:48

    This sample in VB.NET reads all extended properties:

    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
    

    You have to add a reference to Microsoft Shell Controls and Automation from the COM tab of the References dialog.

    0 讨论(0)
  • 2020-11-22 03:50

    Jerker's answer is little simpler. Here's sample code which works 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.
    }
    

    For those who can't reference shell32 statically, you can invoke it dynamically like this:

    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.
    }
    
    0 讨论(0)
提交回复
热议问题