问题描述
我正在尝试从URL(例如"myapp://somestring")启动我的C#应用程序,并且能够做到这一点,但是我仍然不明白如何读取"somestring"值该网址应传递给应用.
I'm trying to start my C# app from a url (example "myapp://somestring") and I've been able to do that, however I still can't understand how to read the "somestring" value that the url should pass to the app.
我尝试了以下代码,但什么也没有:
I tried the following code but nothing:
static void Main (string[] args){
foreach (string arg in args) {
Console.Writeline(arg);
}
}
只需知道,该应用程序已使用适用于Mac的xamarin完成.
Just to know, the app is being done with xamarin for mac.
在此先感谢您的帮助
推荐答案
有时,您希望使用诸如mailto:
或skype:
之类的自定义URL方案来处理一些自定义链接.为此,您可以将应用程序注册到注册表中的URI方案,并创建一个运行的应用程序来处理对该URL方案的请求.
Some times you want to have a custom URL Scheme like mailto:
or skype:
to handle some custom links. To do so, you can register an application to a URI Scheme in registry and create an application that runns to handle the requests to that url scheme.
我已经创建了一个演示该功能的示例.该示例旨在处理myapp:
url方案,并显示一个消息框,其中包含通过url传递给应用程序的值.
I've created an example that demonstrate the feature. This sample is created to handle myapp:
url scheme and show a message box containing the values that is passed though url to the application.
该示例包含2个项目:
- 当单击"myapp:"协议的链接时,将安装并运行Windows Forms应用程序.
- 一个Visual Studio安装项目,该项目将安装应用程序并设置注册设置,以使Windows应用程序能够处理"myapp:"协议.
我想您想创建myapp
url方案,并在c:\myapp.exe
中有一个应用程序,您想使用您的应用程序来处理url方案.然后您应该在注册表/l中创建这些键和值
I suppose you want to create myapp
url scheme and having an application in c:\myapp.exe
which you want to handle the url scheme with your application. Then you should create these keys and values in registry/l
HKEY_CLASSES_ROOT
myapp
(Default) = "URL:myapp"
URL Protocol = ""
DefaultIcon
(Default) = "c:\myapp.exe",0
shell
open
command
(Default) = "c:\myapp.exe" "%1"
然后,您可以使用Environment.GetCommandLineArgs()
获取通过URL传递到应用程序的值并解析参数.
Then you can get the values that pass to the application through the url using Environment.GetCommandLineArgs()
and parse the arguments.
例如,具有url myapp:Hello world!
的URL,应用程序的命令行参数将为myapp:Hello world!
,并且yuou可以对其进行解析并从这些参数中提取所需的信息.
For example having a url myapp:Hello world!
, the command line arguments to your application would be myapp:Hello world!
and yuou can parse it and extract the information that you need from the arguments.
仅作为示例,您可以具有如下网址:myapp:show?form=form1¶m1=something
.然后,您可以解析命令并执行所需的操作.
Just as an example you can have some url like this: myapp:show?form=form1¶m1=something
. Then you can parse the comnmand and do what you need.
1. Windows窗体应用程序在此项目中的作用是什么?
当用户单击注册方案的URL时,应用程序将打开,并且该URL将作为命令行参数传递给应用程序.然后,您可以解析参数并执行所需的操作.
1. What is the role of the Windows Forms Application in this project?
When the user clicks on a url of the registed scheme, the application will open and the url will be passed the the application as command line argument. Then you can parse the arguments and do what you need.
2.如果安装项目,将扮演什么角色?
它将安装处理url方案的应用程序.还会在Windows注册表中使用适当的值注册url方案.
除了使用安装程序项目之外,您还可以使用C#代码创建这些注册键和值,但是使用安装程序项目更方便.如果您没有Visual Studio 2017安装程序项目模板,则可以在此处.
It installs the application that handles the url scheme. Also it registers the url scheme in windows registry with suitable values.
Instead of using an installer projectm, you can create those registery keys and values using C# code as well, but using an installer project is more convinient. If you don't have the Visual Studio 2017 Setup project template, you can download it here.
这篇关于C#从URL启动应用程序并获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!