C# Files (I/O) Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
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
:
Add using
statements:
using Shell32; using System.Runtime.InteropServices;
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); }
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:
Add a using
statement:
using Microsoft.WindowsAPICodePack.Shell; using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
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; } }
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#.
"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)); } }
"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);
"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");
"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");
"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");
"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);
"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
"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();
"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}");
"Manipulating file metadata with Windows API in C#": Manipulating file metadata with Windows API in C#.
// Use P/Invoke to call Windows API functions
"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");
"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);
"Customizing file properties dialog in C#": Customizing file properties dialog in C#.
// Use Windows API or third-party libraries for customization
"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