问题
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);
}
}
Just to know, the app is being done with xamarin for mac.
Thanks in advance for any help
回答1:
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.
Example
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.
The sample contains 2 projects:
- A Windows Forms Application that will be installed and will run when a link of "myapp:" protocole is clicked.
- A Visual Studio Setup Project which installs the application and also setup registery settings to let the windows application handle "myapp:" protocole.
How it works?
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"
Then you can get the values that pass to the application through the url using Environment.GetCommandLineArgs()
and parse the arguments.
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.
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.
FAQ
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. What's the role if Setup project?
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.
来源:https://stackoverflow.com/questions/32445017/c-sharp-start-an-app-from-url-and-get-data