C#实现Web链接启动应用程序

C#实现Web链接启动应用程序

最近需要配合Web端实现用户点击链接来启动应用程序并且需要能够传参数给应用程序。

那么就可以使用注册表来实现这个功能

编写注册表可以在软件安装程序中加入,也可以在软件启动后在软件中编写注册表,在软件安装程序中编写注册表这里就不多说了,这里记录一下C#实现注册表的编写。

if (Registry.ClassesRoot.OpenSubKey(RJCommon.ProtocolName) != null)
                return;
RegistryKey registryKey = Registry.ClassesRoot.CreateSubKey(RJCommon.ProtocolName);
registryKey.SetValue(string.Empty, RJCommon.ProtocolName);//名称为空的是设置默认值
registryKey.SetValue("URL Protocol", Application.ExecutablePath);
registryKey.CreateSubKey("DefaultIcon").SetValue(string.Empty, $"{Application.ExecutablePath},1");
RegistryKey shell = registryKey.CreateSubKey("shell");
shell.SetValue(string.Empty, "open");
RegistryKey open = shell.CreateSubKey("open");
open.SetValue(string.Empty, "open");
open.CreateSubKey("command").SetValue(string.Empty, $""{Application.ExecutablePath}" %1");
registryKey.Close();

这样就实现了软件自注册注册表了。特别注意一下设置默认值时需要键值为空就可以设置默认值了,我在这个地方折腾过,在注册表文件编写中是填写'@'符号,这里使用'@'是不能设置默认值的,切记。那么Web端如何调用呢?也很简单

<a href="RDP:{'ip':'127.0.0.0','user':'user','pwd':'1536'}">打开远程控制</a>

其中RDP就是C#中RJCommon.ProtocolName的值,且必须后面紧跟冒号':'符号,后面就是带的参数了。
C#中接收这些参数就简单了。可以使用static void Main(string[] args)中传入的args参数获取,或者使用Environment.CommandLine来获取,Environment.CommandLine会多传入一个程序路径的值。

注册表文件编写

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOTRDP]  
@="RDP"  
"URL Protocol"="D:\BaiduNetdiskDownload\RdpClient\RdpClient.exe"   
[HKEY_CLASSES_ROOTRDPDefaultIcon]
@="D:\BaiduNetdiskDownload\RdpClient\RdpClient.exe,1"
[HKEY_CLASSES_ROOTRDPshell]
@="open"
[HKEY_CLASSES_ROOTRDPshellopen]
@="open"
[HKEY_CLASSES_ROOTRDPshellopencommand]
@=""D:\BaiduNetdiskDownload\RdpClient\RdpClient.exe" %1"

保存为.reg为后缀的文件,双击执行即可

原文地址:https://www.cnblogs.com/zzr-stdio/p/11062746.html