从URL启动应用程序并获取数据 [英] Start an application from a URL and get data

查看:96
本文介绍了从URL启动应用程序并获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从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.

在此先感谢您的帮助

推荐答案

问题正在寻找适用于macOS的解决方案,提供的答案不适用于该解决方案.但是,对于那些在搜索引擎中找到该帖子并寻找Windows解决方案的人,我将在此处保留答案以供将来参考.

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-从URL打开应用程序

有时候,您希望使用自定义URL方案(例如 mailto: skype:)来处理一些自定义链接.为此,您可以将应用程序注册到注册表中的URI方案,并创建一个运行的应用程序来处理对该URL方案的请求.

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.

我已经创建了一个演示该功能的示例.该示例旨在处理 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个项目:

  • Windows窗体应用程序,当链接"myapp:"时,将安装该Windows窗体应用程序并运行该程序.协议被点击.
  • Visual Studio安装项目,该项目将安装应用程序并设置注册设置,以使Windows应用程序能够处理"myapp:".协议.
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:\n{args}");
            Application.Run(new Form1());
        }
    }
}

它如何工作?

我想您想创建 myapp url方案,并在 c:\ myapp.exe 中有一个应用程序,您希望使用该应用程序来处理url方案.然后,您应该在注册表/l中创建这些键和值

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"

然后,您可以使用 Environment.GetCommandLineArgs()获取通过URL传递到应用程序的值,然后解析参数.

Then you can get the values that pass to the application through the url using Environment.GetCommandLineArgs() and parse the arguments.

例如,如果使用网址 myapp:Hello world!,则应用程序的命令行参数将是 myapp:Hello world!,您可以对其进行解析并提取您需要从参数中获取的信息.

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.

仅作为示例,您可以使用以下网址: myapp:show?form = form1& param1 = something .然后,您可以解析命令并执行所需的操作.

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.

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 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.安装项目的作用是什么?

它将安装处理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 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.

这篇关于从URL启动应用程序并获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆