C# (asp.net) 擷取檔案詳細備忘資訊

來源:互聯網
上載者:User

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

 好了,萬事無憂,放到自己的檔案操作類公用庫裡面去,以後啥時都可以用了

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.