C#通過shell32獲取文件屬性
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Shell32;namespace GetFileCreator { class Program { static void Main(string[] args) { //要獲取屬性的文件路徑 string filePath = @"e:/f/aa.txt"; //初始化Shell接口 Shell32.Shell shell = new Shell32.ShellClass(); //獲取文件所在父目錄對象 Folder folder = shell.NameSpace(filePath.Substring(0, filePath.LastIndexOf('//'))); //獲取文件對應的FolderItem對象 FolderItem item = folder.ParseName(filePath.Substring(filePath.LastIndexOf('//')+1)); //字典存放屬性名和屬性值的鍵值關系對 Dictionary<string, string> Properties = new Dictionary<string, string>(); int i =0; while (true) { //獲取屬性名稱 string key = folder.GetDetailsOf(null, i); if (string.IsNullOrEmpty(key)) { //當無屬性可取時,推出循環 break; } //獲取屬性值 string value = folder.GetDetailsOf(item, i); //保存屬性 Properties.Add(key, value); i++; } } } } </pre>