C#擷取檔案詳細備忘資訊
項目中引用 Shell32.dll;(檔案可以從C:\windows\system32\ 目錄下拷貝,引用後自動識別成 Interop.Shell32)
using System.IO;
using Shell32;
ShellClass sh = new ShellClass();
Folder dir = sh.NameSpace(Path.GetDirectoryName(strPath));
FolderItem item = dir.ParseName(Path.GetFileName(strPath));
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();
進行消化後可以整理這麼個通用方法:
/// <summary> /// 擷取媒體檔案屬性資訊 /// </summary> /// <param name="path">媒體檔案具體路徑</param> /// <param name="icolumn">具體屬性的順序值(-1簡介資訊 1檔案大小 21時間長度 22位元速率)</param> /// <returns></returns> public static string GetMediaDetailInfo(string path,int icolumn) { try { ShellClass sh = new ShellClass(); Shell32.Folder folder = sh.NameSpace(path.Substring(0, path.LastIndexOf("\\"))); Shell32.FolderItem folderItem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 1)); return folder.GetDetailsOf(folderItem, icolumn); } catch (Exception ex) { ex.Message.ToString(); return null; } }
好了,萬事無憂,放到自己的檔案操作類公用庫裡面去,以後啥時都可以用了