How to Read and Write Extended file properties in C#

In C#, you can read and write extended file properties (also known as metadata) using the Shell32 and Microsoft.WindowsAPICodePack.Shell libraries. The first approach uses the COM-based Shell32 library, which is built-in but requires adding a reference manually. The second approach uses the Microsoft.WindowsAPICodePack.Shell library, which provides a more modern and managed API but requires installation through NuGet.

Approach 1: Using Shell32

  • Add a reference to the Shell32.dll:

    • In your Visual Studio project, right-click on "References" in the Solution Explorer.
    • Click "Add Reference..."
    • Go to the "COM" tab, and search for "Microsoft Shell Controls And Automation".
    • Check the box next to it and click "OK".
  • Add using statements:

using Shell32;
using System.Runtime.InteropServices;
  • Create helper methods to read and write extended file properties:
public static string GetExtendedFileProperty(string filePath, int propertyId)
{
    Shell shell = new Shell();
    Folder folder = shell.NameSpace(Path.GetDirectoryName(filePath));
    FolderItem item = folder.ParseName(Path.GetFileName(filePath));

    return folder.GetDetailsOf(item, propertyId);
}

public static void SetExtendedFileProperty(string filePath, int propertyId, string value)
{
    Shell shell = new Shell();
    Folder folder = shell.NameSpace(Path.GetDirectoryName(filePath));
    FolderItem item = folder.ParseName(Path.GetFileName(filePath));

    folder.SetDetailsOf(item, propertyId, value);
}
  • Use the helper methods to read and write extended file properties:
string filePath = @"C:\example.txt";

// Read the "Title" property (propertyId: 21)
string title = GetExtendedFileProperty(filePath, 21);
Console.WriteLine("Title: " + title);

// Set the "Title" property (propertyId: 21)
SetExtendedFileProperty(filePath, 21, "New Title");

Approach 2: Using Microsoft.WindowsAPICodePack.Shell

  • Install the Microsoft.WindowsAPICodePack.Shell NuGet package:

    • In Visual Studio, go to "Tools" > "NuGet Package Manager" > "Manage NuGet Packages for Solution".
    • Search for "Microsoft.WindowsAPICodePack.Shell" and install it to your project.
  • Add a using statement:

using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
  • Create helper methods to read and write extended file properties:
public static string GetExtendedFileProperty(string filePath, string propertyKey)
{
    using (ShellFile shellFile = ShellFile.FromFilePath(filePath))
    {
        return shellFile.Properties.GetProperty(propertyKey).ValueAsObject?.ToString();
    }
}

public static void SetExtendedFileProperty(string filePath, string propertyKey, string value)
{
    using (ShellFile shellFile = ShellFile.FromFilePath(filePath))
    {
        shellFile.Properties.GetProperty(propertyKey).ValueAsObject = value;
    }
}
  • Use the helper methods to read and write extended file properties:
string filePath = @"C:\example.txt";

// Read the "Title" property
string title = GetExtendedFileProperty(filePath, SystemProperties.System.Title);
Console.WriteLine("Title: " + title);

// Set the "Title" property
SetExtendedFileProperty(filePath, SystemProperties.System.Title, "New Title");

These approaches will help you read and write extended file properties in C#.

  1. "C# read extended file properties example": Demonstrating how to read extended file properties in C#.

    using System.IO;
    using System.Linq;
    
    class Program
    {
        static void Main()
        {
            string filePath = "C:\\example.txt";
            var fileProperties = GetFileProperties(filePath);
    
            foreach (var property in fileProperties)
            {
                Console.WriteLine($"{property.Key}: {property.Value}");
            }
        }
    
        static Dictionary<string, object> GetFileProperties(string filePath)
        {
            var fileInfo = new FileInfo(filePath);
            return fileInfo
                .GetType()
                .GetProperties()
                .ToDictionary(prop => prop.Name, prop => prop.GetValue(fileInfo));
        }
    }
    
  2. "Reading and writing file metadata in C#": Reading and writing file metadata using C#.

    // Reading metadata
    string author = File.GetAttributes(filePath).ToString();
    
    // Writing metadata
    File.SetAttributes(filePath, FileAttributes.Hidden);
    
  3. "Working with extended file properties using System.IO in C#": Utilizing System.IO for working with extended file properties in C#.

    using System.IO;
    
    var fileInfo = new FileInfo(filePath);
    Console.WriteLine($"File Size: {fileInfo.Length} bytes");
    
  4. "Accessing file properties with FileInfo class in C#": Accessing file properties with the FileInfo class in C#.

    var fileInfo = new FileInfo(filePath);
    Console.WriteLine($"File Size: {fileInfo.Length} bytes");
    
  5. "Getting extended file information in C#": Getting extended file information using FileInfo in C#.

    var fileInfo = new FileInfo(filePath);
    Console.WriteLine($"File Size: {fileInfo.Length} bytes");
    
  6. "Setting and updating file metadata in C#": Setting and updating file metadata in C#.

    // Setting metadata
    File.SetAttributes(filePath, FileAttributes.Hidden);
    
    // Updating metadata
    File.SetLastWriteTime(filePath, DateTime.Now);
    
  7. "Using Shell32 for extended file property manipulation in C#": Using Shell32 for extended file property manipulation in C#.

    dynamic shellApp = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
    var folder = shellApp.NameSpace(Path.GetDirectoryName(filePath));
    var file = folder.ParseName(Path.GetFileName(filePath));
    string propertyValue = folder.GetDetailsOf(file, 27); // 27 represents a specific property, adjust as needed
    
  8. "Reading and writing custom file properties in C#": Reading and writing custom file properties in C#.

    var dso = new DSOFile.OleDocumentProperties();
    dso.Open(filePath, false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);
    string customPropertyValue = dso.CustomProperties["CustomProperty"].Value.ToString();
    
  9. "Retrieving specific file properties in C#": Retrieving specific file properties using FileInfo in C#.

    var fileInfo = new FileInfo(filePath);
    Console.WriteLine($"Creation Time: {fileInfo.CreationTime}");
    
  10. "Manipulating file metadata with Windows API in C#": Manipulating file metadata with Windows API in C#.

    // Use P/Invoke to call Windows API functions
    
  11. "File properties handling with ShellObject in C#": Handling file properties with ShellObject in C#.

    using Shell32;
    
    Shell shell = new Shell();
    Folder folder = shell.NameSpace(Path.GetDirectoryName(filePath));
    FolderItem folderItem = folder.ParseName(Path.GetFileName(filePath));
    
    Console.WriteLine($"File Size: {folderItem.Size} bytes");
    
  12. "Reading and updating file attributes in C#": Reading and updating file attributes in C#.

    var attributes = File.GetAttributes(filePath);
    attributes |= FileAttributes.ReadOnly;
    File.SetAttributes(filePath, attributes);
    
  13. "Customizing file properties dialog in C#": Customizing file properties dialog in C#.

    // Use Windows API or third-party libraries for customization
    
  14. "Using PropertyHandler to work with extended file properties in C#": Using PropertyHandler to work with extended file properties in C#.

    // Implement PropertyHandler interfaces for custom property handling