使用FileVersionInfo类做一个简单的文件信息查看器

<Window x:Class="WpfApplication1.FileVersionInfoWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="FileVersionInfoWindow" Height="300" Width="300" AllowDrop="True" Drop="Window_Drop" DragEnter="Window_DragEnter">
    <ListView Name="list">
        <ListView.View>
            <GridView x:Name="gridView">
            </GridView>
        </ListView.View>
    </ListView>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;
namespace WpfApplication1
{
    /// <summary>
    /// FileVersionInfoWindow.xaml 的交互逻辑
    /// </summary>
    public partial class FileVersionInfoWindow : Window
    {
        public FileVersionInfoWindow()
        {
            InitializeComponent();
        }
        private void Window_Drop(object sender, DragEventArgs e)
        {
            string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
            List<Tuple<string, object>[]> infos = new List<Tuple<string, object>[]>();
            foreach (var file in files)
            {
                infos.Add(FileVersionHelper.GetInfo(file));
            }
            for (int i = 0; i < 13; i++)
            {
                GridViewColumn column = new GridViewColumn();
                column.Header = FileVersionHelper.names.ToArray()[i].Key;
                column.DisplayMemberBinding = new Binding("[" + i + "].Item2");
                this.gridView.Columns.Add(column);
            }
            this.list.ItemsSource = infos;
        }
        private void Window_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effects = DragDropEffects.Copy;
            }
        }
    }
    class FileVersionHelper
    {
       public 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();
        }
    }
}


知识共享许可协议
《使用FileVersionInfo类做一个简单的文件信息查看器》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     161.74ms