C#使用shell32獲取文件屬性的方法

字號(hào):


    C#使用shell32獲取文件屬性的方法
    這篇文章主要介紹了C#使用shell32獲取文件屬性的方法,涉及C#通過shell32獲取文件屬性的相關(guān)技巧,需要的朋友可以參考下
    本文實(shí)例講述了C#使用shell32獲取文件屬性的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
    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();
    //獲取文件所在父目錄對(duì)象
    Folder folder = shell.NameSpace(filePath.Substring(0, filePath.LastIndexOf('//')));
    //獲取文件對(duì)應(yīng)的FolderItem對(duì)象
    FolderItem item = folder.ParseName(filePath.Substring(filePath.LastIndexOf('//')+1));
    //字典存放屬性名和屬性值的鍵值關(guān)系對(duì)
    Dictionary<string, string> Properties = new Dictionary<string, string>();
    int i =0;
    while (true)
    {
    //獲取屬性名稱
    string key = folder.GetDetailsOf(null, i);
    if (string.IsNullOrEmpty(key))
    {
    //當(dāng)無(wú)屬性可取時(shí),推出循環(huán)
    break;
    }
    //獲取屬性值
    string value = folder.GetDetailsOf(item, i);
    //保存屬性
    Properties.Add(key, value);
    i++;
    }
    }
    }
    }
    希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。