C#获取文件版本号(Exe,Dll)
完整代码如下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; //引入以下命名空间 using System.IO; using System.Diagnostics; namespace GetFileVersion { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog opf = new OpenFileDialog(); opf.Title = "请选择目标文件 文件类型:*.exe | *.dll"; opf.Multiselect = false; opf.Filter = "应用程序(*.exe)|*.exe|类库文件(*.dll)|*.dll"; if (opf.ShowDialog() == DialogResult.OK) textBox1.Text = opf.FileName; } private void button2_Click(object sender, EventArgs e) { if (textBox1.Text.Trim() != "") if (File.Exists(textBox1.Text.Trim())) { //------主要代码 FileVersionInfo ver = FileVersionInfo.GetVersionInfo(textBox1.Text.Trim()); string Version = ver.FileVersion; //------ textBox2.Text = GetFileName(textBox1.Text.Trim()) + " 的版本为:" + Version; } else MessageBox.Show("目标文件错误,请重新选择"); } /// <summary> /// 获取文件名 /// </summary> /// <param name="FilePath">文件完整路径</param> /// <returns></returns> private string GetFileName(string FilePath) { FilePath = FilePath.Substring(FilePath.LastIndexOf("\\")+1); return FilePath; } } }