.NET(C#):使用FileVersionInfo類做一個簡單的檔案資訊查看器

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.