当前位置:网站首页>URL Protocol 网页打开应用程序

URL Protocol 网页打开应用程序

2022-08-09 19:06:00 淡定九号

URL Protocol

引言

有时候在网页开发的时候,会遇到需要打开一个电脑指定程序的功能,这时候就需要用到URL Protocol。

大体的思路就是先给应用程序注册一个自定义的URL Protocol,然后利用URL Protocol实现网页调用应用程序。

1、添加注册表

自定义一个注册表文件,类似于Protocol.reg,添加以下内容

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\MyWinform]
@="URL:MyWinform Protocol Handler"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\MyWinform\DefaultIcon]
@="F:\\AllProjects\\VisualStudio\\TestWinform\\MyWinform\\MyWinform\\bin\\Debug\\MyWinform.exe"
[HKEY_CLASSES_ROOT\MyWinform\shell]
[HKEY_CLASSES_ROOT\MyWinform\shell\open]
[HKEY_CLASSES_ROOT\MyWinform\shell\open\command]
@="\"F:\\AllProjects\\VisualStudio\\TestWinform\\MyWinform\\MyWinform\\bin\\Debug\\MyWinform.exe\" \"%1\""

文件中的“MyWinform”是我自定义的程序名称,可以改成自己的应用程序名称,注意路径这块是双斜杠。

2、编写网页

这里写一个简易的网页,主要就是一个超链接,开头的“MyWinform”就是自定义的程序名称,后面可以传递参数。

<html>
	<body>
		<a href="MyWinform://hello">点我试试</a>
	</body>
</html>

效果

在这里插入图片描述

3、参数接收

这里只说明下Winform下怎么接受参数,其他的各位可以自行研究。

		/// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string inputArgs = string.Join(",", args);
            if (AnotherAppIsRunning())
            {
                MessageBox.Show("已经有程序运行了:" + inputArgs);
            }
            else
            {
                MessageBox.Show(inputArgs);
                Console.WriteLine(inputArgs);
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
                Console.WriteLine("游戏正式开始!!!!");
            }
        }

效果图

在这里插入图片描述

总结

还有一些可以在C# 脚本中添加注册表的方法,但是需要管理员权限,有需要的话可以了解下RegistryKey这个类。

原网站

版权声明
本文为[淡定九号]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yr1102358773/article/details/126252164