たかやまのプログラミング

プログラミング中心のブログです。

C#のMicrosoft.Win32.RegistryKeyクラスを使用してアプリケーションのインストール

takym.code.blog


この記事はhttps://takymsystems.blog.fc2.com/blog-entry-58.htmlに転載しました。
C#でアプリケーションをインストールする方法を見つけました。
Microsoft.Win32.RegistryKeyクラスを使用します。


Install.cs

using Microsoft.Win32;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Sample
{
    public class Install
    {
        public Install()
        {
            // GUID の取得。
            Assembly asm = Assembly.GetExecutingAssembly();
            GuidAttribute ga = ((GuidAttribute)(Attribute.GetCustomAttribute(asm, typeof(GuidAttribute))));
            string guid = ga.Value;

            // アイコンの設定。
            string iconPath = Application.ExecutablePath;
            int iconIndex = 0;

            // アプリケーションの名前を登録。
            string displayName = Application.ProductName;

            // アプリケーションのバージョンを登録。
            string displayVersion = Application.ProductVersion;

            // アプリケーションの場所を登録。
            string location = Application.StartupPath;
            string source = Application.StartupPath;

            // アプリケーションの言語を指定。
            // (※現在は日本語で指定している。)
            int lang = 00000409;
            string publisher = Application.CompanyName;

            // アンインストーラのパス。
            // (※お好みで指定してください。)
            string uninstall = "";


            // ※以下のコードは書き変えないで使ってください。
            RegistryKey rootkey =
                Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");

            RegistryKey regkey = rootkey.CreateSubKey(guid);
            regkey.SetValue("DisplayIcon", iconPath + "," + iconIndex.ToString());
            regkey.SetValue("DisplayName", displayName);
            regkey.SetValue("DisplayVersion", displayVersion);
            regkey.SetValue("InstallLocation", location);
            regkey.SetValue("InstallSource", source);
            regkey.SetValue("Language", lang, RegistryValueKind.DWord);
            regkey.SetValue("Publisher", publisher);
            regkey.SetValue("UninstallPath", "\"" + uninstall + "\"");
            regkey.SetValue("UninstallString", "\"" + uninstall + "\"");
            regkey.Close();
        }
    }
}



ソースコードのダウンロード