Windows中每一個檔案的屬性對話方塊中都有額外的檔案資訊,比如:
System.Diagnostics.FileVersionInfo類可以擷取檔案的資訊。通過靜態方法FileVersionInfo.GetVersionInfo來通過一個檔案路徑構造一個FileVersionInfo對象。
FileVersionInfo的屬性就是檔案資訊的項目,下面是一些常用的檔案屬性對應FileVersionInfo的屬性名稱:
注釋 Comments
公司名稱 CompanyName
檔案名稱 FileName
檔案版本 FileVersion
內部名稱 InternalName
調試版本 IsDebug
補丁版本 IsPatched
語言 Language
著作權 LegalCopyright
商標 LegalTrademark
原始檔案名稱 OriginalFilename
產品名稱 ProductName
產品版本 ProductVersion
另外FileVersionInfo還可以通過ProcessModule類型的FileVersionInfo屬性得到。ProcessModule類型可以通過Process類的MainModule或者Modules屬性得到,通過上述方法,便可以擷取進程的額外檔案資訊。
我寫了一個類:FileVersionHelper,可以快速簡單的擷取熱門檔案資訊,並且返回一組索引值。使用這些索引值可以快速得把資料顯示在介面上:
程式:
FileInfoHelper類:
class FileVersionHelper
{
static Dictionary<string, string> names;
static FileVersionHelper()
{
names = new Dictionary<string, string>()
{
{"注釋", "Comments"},
{"公司名稱", "CompanyName"},
{"檔案名稱", "FileName"},
{"檔案版本", "FileVersion"},
{"內部名稱", "InternalName"},
{"調試版本", "IsDebug"},
{"補丁版本", "IsPatched"},
{"語言", "Language"},
{"著作權", "LegalCopyright"},
{"商標", "LegalTrademarks"},
{"原始檔案名稱", "OriginalFilename"},
{"產品名稱", "ProductName"},
{"產品版本", "ProductVersion"}
};
}
public static Tuple<string, object>[] GetInfo(string file)
{
var info = FileVersionInfo.GetVersionInfo(file);
return names.Select(n => new Tuple<string, object>(
n.Key,
typeof(FileVersionInfo).GetProperty(n.Value).GetValue(info, null) ?? (object)"無")).ToArray();
}
}
原始碼下載
點擊下載
此為微軟SkyDrive存檔,請用瀏覽器直接下載,用某些下載工具可能無法下載
環境:Visual C# 2010 Express