Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

c# - Start an application from a URL and get data

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The question is looking for a solution for macOS and the provided answer will not work for that. But I'll keep the answer here for future reference, for those who find the post in search engines, looking for the solution for Windows.

Windows - Open application from 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.

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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UrlSchemeSample
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var args = "";
            if (Environment.GetCommandLineArgs().Length > 1)
                args = Environment.GetCommandLineArgs()[1];
            MessageBox.Show($"You can decide what to do with the arguments:
{args}");
            Application.Run(new Form1());
        }
    }
}

How does it work?

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 you 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&param1=something. Then you can parse the command 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 registered 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 of 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 project, you can create those registry keys and values using C# code as well, but using an installer project is more convenient. If you don't have the Visual Studio 2017 Setup project template, you can download it here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...