自定義URL Protocol Handler 呼出應用程序


 

緣起: 迅雷,電驢等軟件可以在瀏覽器中點擊一個url后自動啟動,並執行操作。這是咋實現的呢?俺google了許多 ,還是在園子里找到了一個文 http://www.cnblogs.com/hwade/archive/2008/01/08/1029686.html ,這個哥哥喜歡寫繁體字,雖然俺學過書法,認識一些繁體字,但看着還是不爽。
哎!資質愚鈍啊,看了半天沒看太明白,但思路是明白了 ,就是要在注冊表上動手腳。 於是乎繼續google 找到了 http://blogs.gotdotnet.com/noahc/archive/2006/10/19/register-a-custom-url-protocol-handler.aspx 不幸讓我看明白了。
俺簡單的說說俺的理解吧。
一,手動設置注冊表實現


要實現這個功能一共分3步。(我們注冊一個xishui:// 這樣的 protocol-handler,實現在網頁中點擊xishui://hello,就彈出一個對話框,上面顯示“hello”)
1 按照如下結構建立注冊表

其中 [xishui] 是建立在注冊表的 [HKEY_CLASSES_ROOT] 主鍵下。
2 給相關的鍵賦值






大家注意到上面 command 項的值為 c:\test.exe "%1" ,這個"%1"是傳遞給test.exe的參數。如果我們點擊xishui://hello這樣的鏈接 那么%1的值就是“xishui://hello” 這個字符串。

到此我們改寫程序生成test.exe了,我們的目標是彈出一個對話框,顯示xishui://hello鏈接中的hello字樣。 也就是說我們要用正則表達式來取出"xishui://hello" 中 “xishui://” 后面的部分

我們來寫一個控制台程序

using  System;
using  System.IO;
using  System.Windows.Forms;
using  System.Text.RegularExpressions;

namespace  test
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
string key = Regex.Match(args[0], @"(?<=://).+?(?=:|/|\Z)").Value;
            MessageBox.Show(key);
        }

    }

}


讓我把編譯生成的test.exe 復制到c:\下
然后 我寫了個test.html

< href ="xishui://hello" > xishui://hello </ a >


然后我在瀏覽器中點這個鏈接 ,啥效果?你猜



哇咔咔 真的調用了我的test.exe,並且顯示了hello !

 

也可以在運行窗口輸入:xishui://hello 調用

 

二,寫代碼實現

 

View Code
  #region main functions

        /// <summary>
        /// 注冊協議
        /// </summary>
        /// <param name="Root_Key">根節點</param>
        /// <param name="file_application_path">應用程序路徑</param>
        /// <param name="file_application_ico">應用程序打開圖標,可選值</param>
        /// <returns></returns>
        public bool RegeditAdd(string Root_Key, string file_application_path, string file_application_ico)
        {
            RegistryKey reg_CurrentUser = Registry.CurrentUser;
            try
            {
                //獲取注冊表CurrentUser/SOFTWARE/Classes項
                RegistryKey reg_Classes = reg_CurrentUser.OpenSubKey("SOFTWARE", true).OpenSubKey("Classes", true);
                RegistryKey reg_key = reg_Classes.OpenSubKey(Root_Key, true);
                if (reg_key == null)
                {
                    RegistryKey reg_sjbs = reg_Classes.CreateSubKey(Root_Key);
                    //添加默認項
                    reg_sjbs.SetValue("", "URL: " + Root_Key + " Protocol Handler");
                    //協議別名
                    reg_sjbs.SetValue("URL Protocol", file_application_path);
                    RegistryKey reg_DefaultIcon = reg_sjbs.CreateSubKey("DefaultIcon");
                    if (!String.IsNullOrEmpty(file_application_ico) || file_application_ico == "")
                    {
                        //設置自定義圖標
                        reg_DefaultIcon.SetValue("", file_application_ico);
                    }
                    else
                    {
                        //設置系統定義圖標
                        reg_DefaultIcon.SetValue("", file_application_path + ",1");
                    }
                    //呼出處理程序
                    RegistryKey reg_command = reg_sjbs.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command");
                    //%1 表示傳遞的參數,再次%1表示調用處顯示鏈接文本
                    reg_command.SetValue("", "\"" + file_application_path + "\" \"%1\"");
                }
                return true;
            }
            catch { return false; }
            finally { reg_CurrentUser.Close(); }
        }

        /// <summary>
        /// 刪除協議
        /// </summary>
        /// <param name="Root_Key">根節點</param>
        /// <returns></returns>
        public bool RegeditDelete(string Root_Key)
        {
            RegistryKey reg_CurrentUser = Registry.CurrentUser;
            try
            {
                //獲取注冊表CurrentUser/SOFTWARE/Classes項
                RegistryKey reg_Classes = reg_CurrentUser.OpenSubKey("SOFTWARE", true).OpenSubKey("Classes", true);
                RegistryKey reg_sjbs = reg_Classes.OpenSubKey(Root_Key, true);
                if (reg_sjbs != null)
                {
                    reg_Classes.DeleteSubKeyTree(Root_Key);
                    return true;
                }
                return false;
            }
            catch { return false; }
            finally { reg_CurrentUser.Close(); }
        }
        #endregion

        #region control events

        /// <summary>
        /// 協議注冊
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.tabControl1.SelectedIndex == 0)
                {
                    if (this.txt_protocolName.TextLength <= 0)
                    {
                        throw new Exception("請填寫協議名稱!");
                    }
                    if (this.txt_application.TextLength <= 0)
                    {
                        throw new Exception("請選擇需注冊的應用程序,如:.exe 文件");
                    }
                }

                if (!RegeditAdd(this.txt_protocolName.Text.Trim(), this.txt_application.Text.Trim(), this.txt_ico.Text.Trim()))
                    throw new Exception("協議注冊失敗!");
                else { throw new Exception("協議注冊成功!"); }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        /// <summary>
        /// 選擇處理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_browser_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.FileName = "";
            this.openFileDialog1.Filter = "EXE Files (*.exe)|*.exe|All Files (*.*)|*.*";
            this.openFileDialog1.ShowDialog();
            this.txt_application.Text = this.openFileDialog1.FileName;
        }

        /// <summary>
        /// 雙擊瀏覽 選擇處理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_application_DoubleClick(object sender, EventArgs e)
        {
            this.openFileDialog1.FileName = "";
            this.openFileDialog1.Filter = "EXE Files (*.exe)|*.exe|All Files (*.*)|*.*";
            this.openFileDialog1.ShowDialog();
            this.txt_application.Text = this.openFileDialog1.FileName;
        }

        /// <summary>
        /// 選擇處理程序ico
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_browser1_Click(object sender, EventArgs e)
        {
            this.openFileDialog2.FileName = "";
            this.openFileDialog2.Filter = "ICO Files (*.ico)|*.ico|All Files (*.*)|*.*";
            this.openFileDialog2.ShowDialog();
            this.txt_ico.Text = this.openFileDialog2.FileName;
        }

        /// <summary>
        /// 雙擊瀏覽 選擇處理程序ico
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_ico_DoubleClick(object sender, EventArgs e)
        {
            this.openFileDialog2.FileName = "";
            this.openFileDialog2.Filter = "ICO Files (*.ico)|*.ico|All Files (*.*)|*.*";
            this.openFileDialog2.ShowDialog();
            this.txt_ico.Text = this.openFileDialog2.FileName;
        }

        /// <summary>
        /// 刪除協議
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_delete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.tabControl1.SelectedIndex == 1)
                {
                    if (this.txt_protocolName1.TextLength <= 0)
                    {
                        throw new Exception("請慎重填寫協議名稱,以免誤刪除注冊表信息!");
                    }
                }

                if (!RegeditDelete(this.txt_protocolName1.Text.Trim()))
                    throw new Exception("協議卸載失敗,由於您輸入的協議名稱不存在導致卸載失敗!");
                else { throw new Exception("協議卸載成功!"); }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.tabControl1.SelectedIndex == 0)
            {
                this.richtxt_protocolInsert.Text = "注意:您好!注冊協議名稱請不要使用注冊表已經存在的鍵,以免誤操作影響系統正常功能!在運行中輸入【協議名稱://】即可調用或者使用web開發超鏈接中調用!";
            }
            if (this.tabControl1.SelectedIndex == 1)
            {
                this.richtxt_protocolDelete.Text = "注意:您好!卸載協議的時候請檢查協議名稱是否為自己創建的協議,以免誤操作影響系統正常功能!";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.richtxt_protocolInsert.Text = "注意:您好!注冊協議名稱請不要使用注冊表已經存在的鍵,以免誤操作影響系統正常功能!在運行中輸入【協議名稱://】即可調用或者使用web開發超鏈接中調用!";
        }

        #endregion
    }

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM