How to get the params values from the URL scheme which is registered in registry

Dineshkumar.S 446 Reputation points
2023-01-24T07:46:27.64+00:00

I'm trying to start my C# app from a url (example "myapp:?param1=something&param2=somethingelse") and I've been able to do that, however I still can't understand how to read the "something" and "somethingelse" value that the url should pass to the app. can anyone please do give ideas to do that? Thanks in advance

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,288 questions
0 comments No comments
{count} votes

Accepted answer
  1. Reza Aghaei 4,936 Reputation points MVP
    2023-01-24T08:28:28.9666667+00:00

    Assuming you have opened the app from URL using your custom URL scheme, the command-line argument is the URL. FOr example:

    • myapp:?param1=something&param2=somethingelse

    Then add reference to System.Web assembly, create a Uri by passing the command-line arguments to it, and get its Query, then using HttpUtility.ParseQueryString get all the query string parameters.

    Here is the example:

    using System;
    using System.Web;
    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}");
                var uri = new Uri(args);
                var query = HttpUtility.ParseQueryString(uri.Query);
                MessageBox.Show($"param1 is: {query["param1"]}");
                MessageBox.Show($"param2 is: {query["param2"]}");
                Application.Run(new Form1());
            }
        }
    }
    

    Do not forget to add reference to System.Web dll.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful