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

C#のMicrosoft.Win32.RegistryKeyクラスを使用してアプリケーションのインストール – takym.code.blog
こんにちは、Takymです。
http://takym-program.hateblo.jp/entry/20150216/1424085213からの転載記事です。(※一部修正しております。)

以下、記事の内容

C#でアプリケーションをインストールする方法を見つけました。
Microsoft.Win32.RegistryKeyクラスを使用します。

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();
}
}
}

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

Install.zip

コメントを残す

WordPress.com で次のようなサイトをデザイン
始めてみよう