CodePanic! > C#.NET Tips > 今ここ
■アプリケーションのバージョンを取得する
アプリケーションのバージョン情報を取得する方法を2つ紹介します。
アプリケーションの更新通知などの仕組みに利用できると思います。
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.Reflection; // これを追加します Assembly
using System.Diagnostics; // これを追加します FileVersionInfo
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 現在実行中のアセンブリ参照を取得してバージョンを取得する方法
Assembly asmbl = Assembly.GetExecutingAssembly();
AssemblyName name = asmbl.GetName();
MessageBox.Show(name.Version.ToString());
// ファイル名を指定して、そのファイルのバージョンを取得する方法
string filename = @"C:\src\FileMany\FileMany\bin\Release\FileMany.exe";
if (File.Exists(filename))
{
FileVersionInfo filever = FileVersionInfo.GetVersionInfo(filename);
MessageBox.Show(filever.FileVersion);
}
}
}
}
Copyright © 2008.07 - shougo suzaki
|