自定义地址栏协议

it2022-05-05  70

效果演示:打开IE 在地址栏内输入“mailto:”然后回车,系统将打开 OUTLook 软件。 本文就是要达到上面描述的效果。 一、步骤概述 1.将相关信息写入注册表,注册程序关联 2.获取地址栏的内容 二、开始编码 1.将相关信息写入注册表,注册程序关联 1.1.以下是待写入的注册表项和值                     注:MyNode 为注册表自定义节点,@ 为默认键                     1.新建项                     [HKEY_CLASSES_ROOT/MyNode]                     1.1 设置默认键的值                     @="TencentProtocol"                     1.2 创建新键并制定值                     "URL Protocol"="C://app.exe"                                         2.新建项                     [HKEY_CLASSES_ROOT/MyNode/DefaultIcon]                     2.1 设置默认键的值                     @="C://app.exe,1"                                         3.新建项                     [HKEY_CLASSES_ROOT/MyNode/shell]                                     4.新建项                     [HKEY_CLASSES_ROOT/MyNode/shell/open]                                     5.新建项                     [HKEY_CLASSES_ROOT/MyNode/shell/open/command]                     5.1 设置默认键的值                     @="/"C://app.exe/" /"%1/""    1.2以下是具体代码(C#) /*-----------------------------------主程序入口点(Main函数)---------------------------------------------------*/ using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace WindowsFormsApplication1 {     static class Program     {         /// <summary>         /// 应用程序的主入口点。         /// </summary>         [STAThread]         static void Main(string[] args) //接收地址栏内容的命令行参数         {             Application.EnableVisualStyles();             Application.SetCompatibleTextRenderingDefault(false);             Application.Run(new Form1(args));             //Application.Run(new Form2());         }     } } /*------------------------------------业务处理页面--------------------------------------------------*/ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.Win32; namespace WindowsFormsApplication1 {     public partial class Form1 : Form     {         public Form1(string[] args) //接收地址栏内容的命令行参数         {             InitializeComponent();             if (args != null)             {                 foreach (string s in args)                 {                     MessageBox.Show(s);                 }             }             else             {                 MessageBox.Show("命令行参数为null");             }         }         #region 方法         private bool ClearProtocol(string subKeyName)         {             try             {                 RegistryKey key = Registry.ClassesRoot;                 key.DeleteSubKeyTree(subKeyName);                 return true;             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message);                 return false;             }         }         #endregion         #region 递归创建注册表项和值         /// <summary>         /// 构建注册表项         /// </summary>         /// <param name="key">待构建的注册表项对象</param>         /// <returns></returns>         public bool GeneratorReg(List<RegKey> keys)         {             if (keys == null || keys.Count==0)                 return false;             foreach(RegKey key in keys)                 this.RekursionGeneratorReg(key, key.SubKeyName, key.rootKey);             return true;         }         /// <summary>         /// 构建注册表项         /// </summary>         /// <param name="key">待构建的注册表项对象</param>         /// <returns></returns>         ///  <remarks>创建人员(日期):★ben★(100916 15:10)</remarks>         public bool GeneratorReg(RegKey key)         {             if (string.IsNullOrEmpty(key.FullName))                 return false;             this.RekursionGeneratorReg(key, key.SubKeyName, key.rootKey);             return true;         }         /// <summary>         /// 递归创建注册表项         /// </summary>         /// <param name="key">待构建的注册表项对象</param>         /// <param name="subKeyName">注册表子路径(不包含顶级节点名,且不能以斜杠开头)</param>         /// <param name="regKey">注册表键</param>         ///  <remarks>创建人员(日期):★ben★(100916 15:09)</remarks>         private void RekursionGeneratorReg(RegKey key, string subKeyName, RegistryKey regKey)         {             if (string.IsNullOrEmpty(subKeyName))             {                 if (key.RegValus != null)                 {                     foreach (RegValue rv in key.RegValus)                     {                         regKey.SetValue(rv.KeyName.Equals("@") ? "" : rv.KeyName, rv.KeyValue, rv.KeyValueKind);                     }                 }                 return;             }             string currentKeyName = string.Empty;             if (subKeyName.IndexOf(@"/") > -1)             {                 currentKeyName = subKeyName.Substring(0, subKeyName.IndexOf(@"/"));                 subKeyName = subKeyName.Substring(subKeyName.IndexOf(@"/") + 1);             }             else             {                 currentKeyName = subKeyName;                 subKeyName = string.Empty;             }             //1.不包含子节点             if (regKey.OpenSubKey(currentKeyName) == null)             {                 regKey.CreateSubKey(currentKeyName);                 regKey = regKey.OpenSubKey(currentKeyName, true);                 RekursionGeneratorReg(key,subKeyName,regKey);             }             else//2.包含子节点             {                 regKey = regKey.OpenSubKey(currentKeyName, true);                 RekursionGeneratorReg(key, subKeyName, regKey);             }         }         #endregion         #region 事件         private void btnReg_Click(object sender, EventArgs e)         {             List<RegKey> keys = new List<RegKey>();             string appPath = Application.ExecutablePath;             //1.新建项             //[HKEY_CLASSES_ROOT/xxjProtocol]             //1.1 设置默认键的值             //@="TencentProtocol"             //1.2 创建新键并制定值             //"URL Protocol"="C://Program Files//Tencent//QQ//Timwp.exe"                RegKey key;             key = new RegKey(@"HKEY_CLASSES_ROOT/xxjProtocol");             key.RegValus = new List<RegValue>();             key.RegValus.Add(new RegValue("@", "TencentProtocol", RegistryValueKind.String));             key.RegValus.Add(new RegValue("URL Protocol", appPath, RegistryValueKind.String));             keys.Add(key);             //2.新建项             //[HKEY_CLASSES_ROOT/xxjProtocol/DefaultIcon]             //2.1 设置默认键的值             //@="C://Program Files//Tencent//QQ//Timwp.exe,1"             key = new RegKey(@"HKEY_CLASSES_ROOT/xxjProtocol/DefaultIcon");             key.RegValus = new List<RegValue>();             key.RegValus.Add(new RegValue("@", string.Format("{0},1", appPath), RegistryValueKind.String));             keys.Add(key);             //3.新建项             //[HKEY_CLASSES_ROOT/xxjProtocol/shell]             key = new RegKey(@"HKEY_CLASSES_ROOT/xxjProtocol/shell");             keys.Add(key);             //4.新建项             //[HKEY_CLASSES_ROOT/xxjProtocol/shell/open]             key = new RegKey(@"HKEY_CLASSES_ROOT/xxjProtocol/shell/open");             keys.Add(key);             //5.新建项             //[HKEY_CLASSES_ROOT/xxjProtocol/shell/open/command]             //5.1 设置默认键的值             //@="/"C://Program Files//Tencent//QQ//Timwp.exe/" /"%1/""                   key = new RegKey(@"HKEY_CLASSES_ROOT/xxjProtocol/shell/open/command");             key.RegValus = new List<RegValue>();             key.RegValus.Add(new RegValue("@", string.Format("{0} %1", appPath), RegistryValueKind.String));             keys.Add(key);             if (this.GeneratorReg(keys))             {                 MessageBox.Show("注册成功!");             }         }         private void btnClear_Click(object sender, EventArgs e)         {             if (this.ClearProtocol("xxjProtocol"))             {                 MessageBox.Show("清除成功!");             }         }         #endregion     }     /// <summary>     /// 注册表键值对象     /// </summary>     public class RegKey     {         public RegKey(string fullName)         {             FullName = fullName;         }         /// <summary>         /// 注册表顶级节点对象         /// </summary>         ///  <remarks>创建人员(日期):★ben★(100916 15:06)</remarks>         public RegistryKey rootKey         {             get             {                 if (string.IsNullOrEmpty(FullName))                 {                     throw (new Exception(string.Format("无效的注册表路径!{0}", FullName)));                 }                 if (FullName.ToUpper().IndexOf("HKEY_CLASSES_ROOT") > -1)                     return Registry.ClassesRoot;                 else if (FullName.ToUpper().IndexOf("HKEY_CURRENT_USER") > -1)                     return Registry.CurrentUser;                 else if (FullName.ToUpper().IndexOf("HKEY_LOCAL_MACHINE") > -1)                     return Registry.CurrentUser;                 else if (FullName.ToUpper().IndexOf("HKEY_USERS") > -1)                     return Registry.CurrentUser;                 else if (FullName.ToUpper().IndexOf("HKEY_CURRENT_CONFIG") > -1)                     return Registry.CurrentUser;                 else                     throw (new Exception(string.Format("无效的注册表路径!{0}", FullName)));             }         }         /// <summary>         /// 注册表完全路径名         /// </summary>         public string FullName { set; get; }         /// <summary>         /// 注册表完全路径下的键值集合         /// </summary>         public List<RegValue> RegValus {set;get; }         /// <summary>         /// 注册表子集节点路径         /// </summary>         public string SubKeyName         {             get             {                 if (string.IsNullOrEmpty(FullName))                 {                     throw (new Exception(string.Format("无效的注册表路径!{0}", FullName)));                 }                 return FullName.Substring(FullName.IndexOf(@"/") + 1);             }         }     }     /// <summary>     /// 注册表值对象     /// </summary>     ///  <remarks>创建人员(日期):★ben★(100916 15:05)</remarks>     public class RegValue     {         public RegValue(string keyName, string keyValue, RegistryValueKind keyValueKind)         {             KeyName = keyName;             KeyValue = keyValue;             KeyValueKind = keyValueKind;         }         /// <summary>         /// 注册表键名称,默认节点名为""(空)或@         /// </summary>         public string KeyName { set; get; }         /// <summary>         /// 注册表值         /// </summary>         public string KeyValue { set; get; }         /// <summary>         /// 注册表值类型         /// </summary>         public RegistryValueKind KeyValueKind { set; get; }     } } /*--------------------------------------------------------------------------------------*/ 2.获取地址栏的内容 不管你需要运行的是winform 还是 console 程序,使用浏览器地址栏协议打开应用程序后, 如果想获取到地址栏内容,必须在 main 方法加上命令行参数。 namespace WindowsFormsApplication1 {     static class Program     {         /// <summary>         /// 应用程序的主入口点。         /// </summary>         [STAThread]         static void Main(string[] args)         {             Application.EnableVisualStyles();             Application.SetCompatibleTextRenderingDefault(false);             Application.Run(new Form1(args));             //Application.Run(new Form2());         }     } } 3.基本完成这两步操作,浏览器地址栏协议就已经完成了。

转载于:https://www.cnblogs.com/xxj-jing/archive/2010/09/17/2890112.html

相关资源:DirectX修复工具V4.0增强版

最新回复(0)