[VC2005] アプリケーションのバージョン情報ダイアログボックスを作る方法

◆概要

この資料は、Microsoft(R) Visual C# 2005で アプリケーションのバージョン情報ダイアログボックスを作る方法について記述しています。
アプリケーションのバージョン情報ダイアログボックスでは、アプリケーションのバージョンや会社名、製品名、Copyright、製品の概要などを表示します。

◆フォームに配置するもの

ラベル labelProductName
labelVersion
labelCopyright
labelCompanyName
テキストボックス textBoxDescription
コマンドボタン btnOK

◆usingディレクティブに次の1行を追加

using System.Reflection;


◆Sample

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //アセンブリ情報からの製品情報を表示する情報ボックスを
            //初期化します。
            //アプリケーションのアセンブリ情報設定を
            //次のいずれかにて変更します:
            //- [プロジェクト] メニューの [プロパティ] にある 
            //[アプリケーション] の [アセンブリ情報]
            //  - AssemblyInfo.cs
            this.Text = String.Format(
                     "{0} のバージョン情報", 
            AssemblyTitle);
            this.labelProductName.Text = AssemblyProduct;
            this.labelVersion.Text = String.Format(
                     "バージョン {0}", AssemblyVersion);
            this.labelCopyright.Text = AssemblyCopyright;
            this.labelCompanyName.Text = AssemblyCompany;
            this.textBoxDescription.Text = AssemblyDescription;
        }

        #region アセンブリ属性アクセサ

        public string AssemblyTitle
        {
            get
            {
                // このアセンブリ上のタイトル属性をすべて取得します
                object[] attributes =
                Assembly.GetExecutingAssembly().
                     GetCustomAttributes(
                         typeof(AssemblyTitleAttribute), 
                         false
                         );
                // 少なくとも 1 つのタイトル属性がある場合
                if (attributes.Length > 0)
                {
                    // 最初の項目を選択します
                    AssemblyTitleAttribute titleAttribute =
                    (AssemblyTitleAttribute)attributes[0];
                    // 空の文字列の場合、その項目を返します
                    if (titleAttribute.Title != "")
                        return titleAttribute.Title;
                }
                // タイトル属性がないか、またはタイトル属性が空の文字列
                //の場合、exe 名を返します
                return
                System.IO.Path.GetFileNameWithoutExtension(
                     Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return
                Assembly.GetExecutingAssembly().GetName().
                Version.ToString();
            }
        }

        public string AssemblyDescription
        {
            get
            {
                // このアセンブリ上の説明属性をすべて取得します
                object[] attributes =
                Assembly.GetExecutingAssembly().
                   GetCustomAttributes(
                      typeof(AssemblyDescriptionAttribute), 
                      false);
                // 説明属性がない場合、空の文字列を返します
                if (attributes.Length == 0)
                    return "";
                // 説明属性がある場合、その値を返します
                return
                ((AssemblyDescriptionAttribute)attributes[0]).
                       Description;
            }
        }

        public string AssemblyProduct
        {
            get
            {
                // このアセンブリ上の製品属性をすべて取得します
                object[] attributes =
                Assembly.GetExecutingAssembly().
                            GetCustomAttributes(
                            typeof(AssemblyProductAttribute), 
                            false);
                // 製品属性がない場合、空の文字列を返します
                if (attributes.Length == 0)
                    return "";
                // 製品属性がある場合、その値を返します
                return ((AssemblyProductAttribute)attributes[0])
                .Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                // このアセンブリ上の著作権属性をすべて取得します
                object[] attributes =
                Assembly.GetExecutingAssembly().
                        GetCustomAttributes(
                        typeof(AssemblyCopyrightAttribute), 
                        false);
                // 著作権属性がない場合、空の文字列を返します
                if (attributes.Length == 0)
                    return "";
                // 著作権属性がある場合、その値を返します
                return
        ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyCompany
        {
            get
            {
                // このアセンブリ上の会社属性をすべて取得します
                object[] attributes =
                Assembly.GetExecutingAssembly().
                       GetCustomAttributes(
                          typeof(AssemblyCompanyAttribute), 
                           false);
                // 会社属性がない場合、空の文字列を返します
                if (attributes.Length == 0)
                    return "";
                // 会社属性がある場合、その値を返します
                return ((AssemblyCompanyAttribute)attributes[0])
                .Company;
            }
        }
        #endregion

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}

▼ページトップへ