C# (asp.net) 获取文件详细备注信息
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; } }
好了,万事无忧,放到自己的文件操作类公用库里面去,以后啥时都可以用了