萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

C# Shell32 獲取檔案詳細屬性:標題、作者、修改時間等

需求背景

.NET FileInfo 僅提供基本屬性 (大小、建立時間、屬性)。要獲取 Office 文件的標題、作者、關鍵字、相機拍攝參數、音訊編碼等**擴充屬性**,需透過 Windows Shell 命名空間 (Shell32)。

前置準備

  1. 專案 → 加入參考 → COM → Microsoft Shell Controls And Automation (Shell32.dll)
  2. 命名空間:using Shell32;

完整程式碼


using System;
using System.Collections.Generic;
using Shell32;

namespace GetFileProperties
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"e:\f\aa.txt";  // 修改為實際檔案路徑
            
            // 1. 取得父目錄 Shell Folder 物件
            string folderPath = System.IO.Path.GetDirectoryName(filePath);
            string fileName = System.IO.Path.GetFileName(filePath);
            
            Shell32.Shell shell = new Shell32.ShellClass();
            Folder folder = shell.NameSpace(folderPath);
            
            // 2. 取得檔案對應的 FolderItem
            FolderItem item = folder.ParseName(fileName);
            if (item == null)
            {
                Console.WriteLine("找不到檔案");
                return;
            }
            
            // 3. 遍歷所有屬性索引 (0~300+)
            Dictionary properties = new Dictionary();
            
            for (int i = 0; i < 300; i++)
            {
                string propName = folder.GetDetailsOf(null, i);
                if (string.IsNullOrEmpty(propName))
                    break;  // 連續空白視為結束
                
                string propValue = folder.GetDetailsOf(item, i);
                if (!string.IsNullOrEmpty(propValue))
                {
                    properties[propName] = propValue;
                    Console.WriteLine($"{propName}: {propValue}");
                }
            }
            
            // 4. 常用屬性快速存取
            Console.WriteLine("\n--- 關鍵屬性 ---");
            Console.WriteLine($"名稱: {properties.GetValueOrDefault("名稱", "N/A")}");
            Console.WriteLine($"大小: {properties.GetValueOrDefault("大小", "N/A")}");
            Console.WriteLine($"類型: {properties.GetValueOrDefault("類型", "N/A")}");
            Console.WriteLine($"修改時間: {properties.GetValueOrDefault("修改時間", "N/A")}");
            Console.WriteLine($"建立時間: {properties.GetValueOrDefault("建立時間", "N/A")}");
            
            // Office 文件特有
            Console.WriteLine($"標題: {properties.GetValueOrDefault("標題", "N/A")}");
            Console.WriteLine($"作者: {properties.GetValueOrDefault("作者", "N/A")}");
            Console.WriteLine($"關鍵字: {properties.GetValueOrDefault("關鍵字", "N/A")}");
            Console.WriteLine($"備註: {properties.GetValueOrDefault("備註", "N/A")}");
        }
    }
}

常見屬性索引對照表 (Windows 10/11)

索引屬性名稱 (中文)屬性名稱 (英文)說明
0名稱Name檔案名
1大小Size檔案大小
2類型Type檔案類型描述
3修改時間Date modified最後修改時間
4建立時間Date created建立時間
5存取時間Date accessed最後存取時間
10屬性Attributes唯讀、隱藏等
12標題Title文件標題 (Office)
13主題Subject
14評分Rating星級
15標籤Tags關鍵字/標籤
16類別Categories
17作者Authors文件作者
18備註Comments
20相機製造商Camera maker照片 EXIF
21相機型號Camera model
27拍攝日期Date taken照片拍攝時間
30寬度Width圖片/影片寬度
31高度Height圖片/影片高度
40長度Length音訊/影片長度
41比特率Bit rate音訊品質
42音樂類型Genre
43表演者Performers
44專輯Album
45年份Year
46曲目Track number

進階:封裝為通用工具類


public static class ShellFileProperty
{
    public static Dictionary GetAllProperties(string filePath)
    {
        var result = new Dictionary();
        var folderPath = System.IO.Path.GetDirectoryName(filePath);
        var fileName = System.IO.Path.GetFileName(filePath);
        
        var shell = new Shell32.ShellClass();
        var folder = shell.NameSpace(folderPath);
        var item = folder.ParseName(fileName);
        if (item == null) return result;
        
        for (int i = 0; i < 500; i++)
        {
            var name = folder.GetDetailsOf(null, i);
            if (string.IsNullOrEmpty(name)) continue;
            var value = folder.GetDetailsOf(item, i);
            if (!string.IsNullOrEmpty(value))
                result[name] = value;
        }
        return result;
    }
}

注意事項

  • 需在 STA 執行緒 (WinForms/WPF 主執行緒預設為 STA),Console 應用需標記 [STAThread]。
  • 屬性名稱隨系統語言不同 (中文/英文),建議雙語支援或使用 Canonical Name (如 System.Title)。
  • 大量檔案批次讀取效能較慢,考慮非同步或快取。
  • .NET Core / .NET 5+ 於非 Windows 平台無法使用 COM,需改用 MetadataExtractor、TagLib# 等純 managed 庫。
copyright © 萬盛學電腦網 all rights reserved