1. 程式人生 > >C#通過shell32獲取檔案詳細備註資訊

C#通過shell32獲取檔案詳細備註資訊

1.從系統Window/System32資料夾中Copy出 Shell32.dll Com元件

將Shell32.dll檔案引用到專案中,並設定“嵌入互操作型別”為false

2.程式碼例項:

ShellClass sh = new ShellClass();
Folder dir = sh.NameSpace(Path.GetDirectoryName(filename));
FolderItem item = dir.ParseName(Path.GetFileName(filename));
StringBuilder sb = new StringBuilder();
for (int i = -1; i < 50; i++)
{
    // 0 Retrieves the name of the item. 
    // 1 Retrieves the size of the item. 
    // 2 Retrieves the type of the item. 
    // 3 Retrieves the date and time that the item was last modified. 
    // 4 Retrieves the attributes of the item. 
    // -1 Retrieves the info tip information for the item. 
    sb.Append(i.ToString());
    sb.Append(":");
    sb.Append(dir.GetDetailsOf(item, i));
    sb.Append("/r/n");
}
string c = sb.ToString();
索引說明(視訊檔案常用屬性):

0--檔名稱

1---檔案大小
2---檔案型別
3---修改時間
4---建立時間
8---可用性
27---時長

28--位元率

303--資料速率

304--幀高度

305--幀速率

306--幀寬度

307--視訊方向

308--總位元率

3.程式碼例項(有不可取的地方):

//初始化Shell介面
ShellClass sh = new ShellClass();
//獲取檔案所在父目錄物件
Folder dir = sh.NameSpace(Path.GetDirectoryName(filename));
//獲取檔案物件的FolderItem物件
FolderItem item = dir.ParseName(Path.GetFileName(filename));
//字典存放屬性名和屬性值
Dictionary<string, string> dic = new Dictionary<string, string>();
//迴圈獲取詳細資訊
int i = 0;
while (true)
{
    //獲取屬性名稱
    string key = dir.GetDetailsOf(null,i);
    if (string.IsNullOrEmpty(key))
    {
        //當無屬性可取時,退出迴圈
        break;
    }
    //獲取屬性值
    string value = dir.GetDetailsOf(item,i);
    dic.Add(key,value);
    i++;
}
listBox.ItemsSource = dic;

更多: