C# (asp.net) 获取文件详细备注信息

本文介绍了一种使用C#和Shell32.dll库获取文件详细属性的方法,包括文件大小、类型、最后修改日期等,并提供了一个通用方法GetMediaDetailInfo用于获取指定媒体文件的特定属性。

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;
      }
  }

 好了,万事无忧,放到自己的文件操作类公用库里面去,以后啥时都可以用了

 

转载于:https://www.cnblogs.com/wjg121/archive/2011/11/10/2244673.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值