.NET FileInfo 僅提供基本屬性 (大小、建立時間、屬性)。要獲取 Office 文件的標題、作者、關鍵字、相機拍攝參數、音訊編碼等**擴充屬性**,需透過 Windows Shell 命名空間 (Shell32)。
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")}");
}
}
}
| 索引 | 屬性名稱 (中文) | 屬性名稱 (英文) | 說明 |
|---|---|---|---|
| 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;
}
}
[STAThread]。System.Title)。MetadataExtractor、TagLib# 等純 managed 庫。