js调用winform程序(带参数)
2024-10-18 22:35:14
我们会发现,我们点击迅雷下载的时候 网页可以调用应用程序,而且连接会传入迅雷,这个是怎么做到的呢?
原理: 先注册表中添加软件的具体信息,然后通过 href 可以直接调用
1.写入注册表信息,注册,如果不需要参数 只要第一个HKEY_CLASSES_ROOT\test 段落
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\test]
"URL Protocol"="C://xx//xx.exe" [HKEY_CLASSES_ROOT\test\DefaultIcon]
@="C://xx//xx.exe" [HKEY_CLASSES_ROOT\test\shell] [HKEY_CLASSES_ROOT\test\shell\open] [HKEY_CLASSES_ROOT\test\shell\open\command]
@="C:\\xx\\xx.exe %1"
winfrom写入注册表(模拟上面直接注册注册表,减少用户操作)
using Microsoft.Win32;
//使用CreateSubKey()HKEY_CLASSES_ROOT下创建子项
RegistryKey hklm = Registry.ClassesRoot;
RegistryKey key = hklm.CreateSubKey(@"test");
key.SetValue("URL Protocol", "C://xx//xx.exe"); RegistryKey key2 = key.CreateSubKey(@"DefaultIcon");
key2.SetValue("", "C://xx//xx.exe"); RegistryKey key3 = key.CreateSubKey(@"shell");
RegistryKey key4 = key3.CreateSubKey(@"open");
RegistryKey key5 = key4.CreateSubKey(@"command");
key5.SetValue("", "C:\\xx\\xx.exe %1"); hklm.Close();
key.Close(); MessageBox.Show("初始化成功");
2.关联
html端
var sessionId = "1"; location.href = "xx://" + sessionId;
winform端,在Program.cs 修改main方法
[STAThread]
static void Main(string[] args)
{ if (args.Length > 0)
{
MessageBox.Show("来了");
MessageBox.Show(args[0].ToString());
} Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main());
}
--------------------------------其他注册表操作介绍---------------------------------------------------------
注册表巢
在注册表中,最上面的节点是注册表巢(registry hive)。
HKEY_CLASSES_ROOT(HKCR) 包含系统文件类型的细节,以及应用程序可以打开的文件类型,它还包含所有COM组件的注册信息。
HKEY_CURRENT_USER(HKCU) 包含用户目前登陆的机器的用户配置,包括桌面设置、环境变量、网络和打印机连接和其他定义用户操作环境的变量。
HKEY_LOCAL_MACHINE(HKLM) 是一个很大的巢,其中包含所有安装到机器上的软件和硬件的信息。
HKEY_USERS(HKUSR) 包含所有用户的用户配置。
HKEY_CURRENT_CONFIG(HKCF) 包含机器上硬件的信息。
二.注册表类及常用属性和函数
using Microsoft.Win32;
这个命名空间包含了注册表相关的类。Registry类、RegistryKey类。
Registry类封装了注册表的七个基本主键:
Registry.ClassesRoot |
对应于HKEY_CLASSES_ROOT主键 |
Registry.CurrentUser |
对应于HKEY_CURRENT_USER主键 |
Registry.LocalMachine |
对应于 HKEY_LOCAL_MACHINE主键 |
Registry.User |
对应于 HKEY_USER主键 |
Registry.CurrentConfig |
对应于HEKY_CURRENT_CONFIG主键 |
Registry.DynDa |
对应于HKEY_DYN_DATA主键 |
Registry.PerformanceData |
对应于HKEY_PERFORMANCE_DATA主键 |
RegistryKey类封装了对注册表的基本操作。包括读、写、删等操作的常用函数:
Name | 键的名称(只读) |
SubKeyCount | 键的子键个数 |
ValueCount | 键包含的值的个数 |
Close() | 关闭键 |
CreateSubKey() | 创建给定名称的子键 |
DeleteSubKey() | 删除指定的子键 |
DeleteSubKeyTree() | 递归删除子键及其所有的子键 |
DeleteValue() | 从键中删除一个指定的值 |
GetAccessControl() | 返回指定注册表键的访问控制表 |
GetSubKeyNames() | 返回包含子键名称的字符串数组 |
GetValue() | 返回指定的值 |
GetValueKind() | 返回指定的值,检索其注册表数据类型 |
GetValueNames() | 返回一个包含所有键值名称的字符串数组 |
OpenSubKey() | 返回表示给定子键的RegistryKey实例引用 |
SetAccessControl() | 把访问控制表(ACL)应用于指定的注册表键 |
SetValue() | 设置指定的值 |
注册表项的创建、打开、删除
1,创建
//使用CreateSubKey()在SOFTWARE下创建子项test
RegistryKey hklm = Registry.LocalMachine;
RegistryKey hkSoftWare = hklm.CreateSubKey(@"SOFTWARE\test");
hklm.Close();
hkSoftWare.Close();
2,打开
//使用OpenSubKey()打开项,获得RegistryKey对象,当路径不存在时,为Null。第二个参数为true,表示可写,可读,可删;省略时只能读。
RegistryKey hklm = Registry.LocalMachine;
RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\test",true);
hklm.Close();
hkSoftWare.Close();
3,删除
//主要用到了DeleteSubKey(),删除test项
RegistryKey hklm = Registry.LocalMachine;
hklm.DeleteSubKey(@"SOFTWARE\test", true); //为true时,删除的注册表不存在时抛出异常;当为false时不抛出异常。
hklm.Close();
四、注册表键值的创建、打开和删除
1,创建
//主要用到了SetValue(),表示在test下创建名称为Name,值为RegistryTest的键值。第三个参数表示键值类型,省略时,默认为字符串
RegistryKey hklm = Registry.LocalMachine;
RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\test",true);
hkSoftWare.SetValue("Name", "RegistryTest", RegistryValueKind.String);
hklm.Close();
hkSoftWare.Close();
2,打开
//主要用到了GetValue(),获得名称为"Name"的键值
RegistryKey hklm = Registry.LocalMachine;
RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\test", true);
string sValue = hkSoftWare.GetValue("Name").ToString();
hklm.Close();
hkSoftWare.Close();
3,删除
//主要用到了DeleteValue(),表示删除名称为"Name"的键值,第二个参数表示是否抛出异常
RegistryKey hklm = Registry.LocalMachine;
RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\test", true);
hkSoftWare.DeleteValue("Name", true);
hklm.Close();
hkSoftWare.Close();
五、判断注册表项、注册表键值是否存在
//判断注册表项是否存在
private bool IsRegistryKeyExist(string sKeyName)
{
string[] sKeyNameColl;
RegistryKey hklm = Registry.LocalMachine;
RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE");
sKeyNameColl = hkSoftWare.GetSubKeyNames(); //获取SOFTWARE下所有的子项
foreach (string sName in sKeyNameColl)
{
if (sName == sKeyName)
{
hklm.Close();
hkSoftWare.Close();
return true;
}
}
hklm.Close();
hkSoftWare.Close();
return false;
} //判断键值是否存在
private bool IsRegistryValueNameExist(string sValueName)
{
string[] sValueNameColl;
RegistryKey hklm = Registry.LocalMachine;
RegistryKey hkTest = hklm.OpenSubKey(@"SOFTWARE\test");
sValueNameColl = hkTest.GetValueNames(); //获取test下所有键值的名称
foreach (string sName in sValueNameColl)
{
if (sName == sValueName)
{
hklm.Close();
hkTest.Close();
return true;
}
}
hklm.Close();
hkTest.Close();
return false;
}
六、程序自启动程序
//开启程序自启动
string path = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.SetValue("JcShutdown", path);
rk2.Close();
rk.Close(); //关闭程序自启动
string path = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.DeleteValue("JcShutdown", false);
rk2.Close();
rk.Close();
最新文章
- ionic 里使用 iframe 可能遇到的问题
- Linux - 修复Ubuntu错误“System program problem detected”
- 使用XmlHelper添加节点C#代码
- 如何查看PowerShell版本
- MySQL安装之“测试”
- debian7安装oracle11g
- Android(java)学习笔记186:对ListView等列表组件中数据进行增、删、改操作
- 【LeetCode】Repeated DNA Sequences 解题报告
- 至Android虚拟机发送短信和拨打电话
- SVG 学习(一)
- linux下安装PHP5.5
- ftp资源调用迅雷下载
- Linux之旅-ubuntu下搭建nodejs环境
- Qt Creator简单计算器的Demo
- HIve:beeline终端上在输错hive语句时,无论 Backspace还是delete 都删除不掉错误的语句,没有办法退格
- MySql连接空闲8小时自动断开引起的问题
- LEAPMOTION开发UI专题(1)
- Asp.Net Core探索 之 appsettings.json
- JVM内存管理---垃圾收集器
- javascript 替换 window.onload 的 document.ready
热门文章
- go语言学习-结构体
- 基于zepto的移动端轻量级日期插件
- hihoCoder.1509.异或排序(位运算 思路)
- HDU.2829.Lawrence(DP 斜率优化)
- JavaScript_几种继承方式(2017-07-04)
- spring cloud——feign为GET请求时的对象参数传递
- print2flash文档在线预览应用(java,.net)
- 状态压缩+矩阵乘法hdu-4332-Constructing Chimney
- 设备树中ranges属性分析(1)
- StackExchange.Redis 管道 批量 高性能插入数据