在Windows 10中将应用程序注册到URI方案

问题描述:

几年前,我开发了一个从ASP.net Web应用程序中调用的Silverlight组件,该组件使用PInvoke访问客户端计算机上的USB(串行COM端口)以允许向某些扫描仪硬件发送命令.

A few years back I developed a Silverlight Component called from within an ASP.net web app, that uses PInvoke to access a USB (Serial COM port) on the client machine to allow for sending commands to some scanner hardware.

随着Windows 10的到来和Silverlight的必然消亡,我正在寻找访问客户端PC上的硬件的替代方法(这是所有Intranet Web应用程序的东西,我们对实现有很多控制权)

With the advent of Windows 10 and the inevitable demise of Silverlight I am looking for alternatives to accessing hardware on the client PC (This is all Intranet Web Application stuff where we have a lot of control over the implementation)

目前,我正在按照此页面查看将应用程序注册到URI方案(简易解决方案): https://connect.microsoft.com/IE/feedback/details/864863/documented-api-function-navigator-mslaunchuri-not-present-in-windows-7

Currently I am looking at Registering an Application to a URI Scheme (Easy solution) as per this page:https://msdn.microsoft.com/library/aa767914(v=vs.85).aspx OR alternatively maybe Javascript navigator.msLaunchUri (This seems to not be supported in Windows 7, which we need to still support) Refer: https://connect.microsoft.com/IE/feedback/details/864863/documented-api-function-navigator-mslaunchuri-not-present-in-windows-7

将应用程序注册到URI方案在Windows 7/8/8.1中可以正常工作,但在Windows 10中似乎已更改-有人知道我可以如何实现此目的(通过C#代码,注册表等)来允许此操作在Windows 10中工作

The Registering of an Application to a URI Scheme works fine in Windows 7/8/8.1 but seems to have changed in Windows 10 - Does anyone know how I can implement this (Through C# code, registry, something) to allow this to work in Windows 10

我最近也想这样做-我找到了答案,所以我将其发布在这里以备将来参考,但找不到有效的示例在任何地方都可以使用C#.

I recently wanted to just this as well - and I found the answer so i'm posting it here for future reference and I couldn't find a working example in C# anywhere.

首先,您的应用需要requireAdministrator权限.为此,请在解决方案资源管理器中右键单击该项目,然后单击Add New Item,然后选择General,最后选择Application Manifest文件(如果还没有文件).在其中,将requestedExecutionLevel更改为requireAdministrator.保存.

First of all your app needs requireAdministrator permissions. To do this, right click on the project in the Solution Explorer and click Add New Item, then select General and finally Application Manifest file if you don't already have one. In there, change the requestedExecutionLevel to requireAdministrator. Save.

这是您第一次执行此操作,您可能需要重新启动Visual Studio,因为它可能不是在管理员权限下运行的.

I this is the first time you've done this, you'll need to restart Visual Studio as it probably isnt running under Admin privaleges.

好的,所以我希望我的应用在启动时创建注册表项,因此在窗体的构造函数中,我输入了以下代码,该代码为名为'oggsplit.exe的程序创建了URL协议foo://. '(我恰好在C:根目录中,所以我只是用它来进行测试)

Okay, so I wanted my app to create the registry key when it starts up, so in the constructor for my form I put in the following code, which creates the URL Protocol foo:// for a program called 'oggsplit.exe' (which I happened to have in my C: root so I just used that for testing)

RegistryKey key;
key = Registry.ClassesRoot.CreateSubKey("foo");
key.SetValue("", "URL: Foo Protocol");
key.SetValue("URL Protocol","");

key = key.CreateSubKey("shell");
key = key.CreateSubKey("open");
key = key.CreateSubKey("command");
key.SetValue("", "C:\\oggsplit.exe");

配置完成后,保存并运行该程序.您将不会获得任何反馈,只要您没有看到任何错误,它就应该可以正常工作.现在,打开浏览器(无需重新启动或执行任何操作),然后转到地址foo://hello.这是我在Google Chrome浏览器中的样子:

Once you've configured that, save and run the program. You'll get no feedback, and as long as you don't see any errors it should have worked correctly. Now, open your browser (no need to restart or anything) and go to the address foo://hello. This is what it looks like for me in Google Chrome:

然后它将询问您是否要从浏览器中打开应用程序,单击确定".Presto,您的应用程序从浏览器打开,您现在可以在网页中放置一个专用链接,以从浏览器打开您的应用程序.

It will then ask you if you want to open your application from the browser, click okay. Hey Presto, your app opens from the browser, you can now put a specilised link into your web page to open your app from the browser. This Page also documents how to pass arguments through to your program as well.