2012-03-23 105 views
0

任何人都可以幫助我如何從Windows對象,即FileSize,FileType,Year,Label,DateModified,FileVersion獲取文件屬性。我試圖訪問FileInfo類中的信息,它似乎沒有我正在尋找的所有必要屬性。我可以用哪些其它的庫來訪問這些信息,如果您能提供的例子,謝謝從Windows文件系統讀取文件屬性?

+1

試試這個http://stackoverflow.com/questions/220097/read-write-extended-file-properties-c 或 http://www.codeproject.com/Articles/5036/ID3-Tag -Reader - 使用殼功能 – Mohit 2012-03-23 07:15:48

回答

1

有些是在FileInfo中已繳費(長度是文件大小,修改日期只是LastWriteTime)。其中一些信息可從FileVersionInfo獲得。 '類型'有點棘手,但this後有一些關於在註冊表中查找mime類型的信息。這對我在Windows 7上工作:

private static string GetType(string fileName) 
    { 
     string type = "Unknown"; 
     string ext = System.IO.Path.GetExtension(fileName).ToLower(); 
     Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); 
     if (regKey != null && regKey.GetValue("") != null) 
     { 
      string lookup = regKey.GetValue("").ToString(); 
      if (!string.IsNullOrEmpty(lookup)) 
      { 
       var lookupKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(lookup); 
       if (lookupKey != null) 
       { 
        type = lookupKey.GetValue("").ToString(); 
       } 
      } 
     } 
     return type; 
    } 

它會產生您在文件屬性的詳細信息標籤頁中看到的類型。例如,用於exe的'Application'和用於bmp的'Bitmap Image'。

答案here使用windows api函數shgetfileinfo獲取類型。