C#のMicrosoft.Win32.RegistryKeyクラスを使用してプロトコルの登録

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

以下、記事の内容

C#でプロトコルの登録する方法を見つけました。
Microsoft.Win32.RegistryKeyクラスを使用します。

using Microsoft.Win32;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Sample
{
public class Protocol
{
public Protocol()
{
// GUIDの取得。
Assembly asm = Assembly.GetExecutingAssembly();
GuidAttribute ga = ((GuidAttribute)(Attribute.GetCustomAttribute(asm, typeof(GuidAttribute))));
string guid = ga.Value;
// 登録するプロトコル。
string ProtcolType = "test";
// 実行するコマンドライン。
string commandline = "\"" + Application.ExecutablePath + "\" \"%1\"";
// 説明。
string description1 = "test: Test Protocol Handler";
string description2 = "URL:test Protocol";
// 動詞。
string verb = "open";
// アイコンの指定。
string iconPath = Application.ExecutablePath;
int iconIndex = 0;
// ※以下のコードは書き変えないで使ってください。
RegistryKey rootkey = Registry.ClassesRoot;
RegistryKey regkey = rootkey.CreateSubKey("PROTOCOLS\\Handler\\" + ProtcolType);
regkey.SetValue("", description1);
regkey.SetValue("CLSID", guid);
regkey.Close();
RegistryKey typekey = rootkey.CreateSubKey(ProtcolType);
typekey.SetValue("", description2);
typekey.SetValue("Source Filter", guid);
typekey.SetValue("URL Protocol", "");
typekey.Close();
RegistryKey verblkey = rootkey.CreateSubKey(ProtcolType + "\\shell\\");
verblkey.SetValue("", verb);
verblkey.Close();
RegistryKey open = rootkey.CreateSubKey(ProtcolType + "\\shell\\" + verb);
open.SetValue("CommandId", "");
open.SetValue("DontReturnProcessHandle", "");
open.Close();
RegistryKey cmdkey = rootkey.CreateSubKey(ProtcolType + "\\shell\\" + verb + "\\command");
cmdkey.SetValue("", commandline);
cmdkey.SetValue("DelegateExecute", guid);
cmdkey.Close();
RegistryKey iconkey = rootkey.CreateSubKey(ProtcolType + "\\DefaultIcon");
iconkey.SetValue("", iconPath + "," + iconIndex.ToString());
iconkey.Close();
}
}
}

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

Protocol.zip

コメントを残す

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