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

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

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

takym.code.blog


この記事はhttps://takymsystems.blog.fc2.com/blog-entry-60.htmlに転載しました。
C#プロトコルの登録する方法を見つけました。
Microsoft.Win32.RegistryKeyクラスを使用します。


Protocol.cs

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



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