Click here to Skip to main content
16,022,924 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i'm trying to get custom protocol value inside my winform,

i used this code to register the keys
C#
using (var key = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Classes\\" + "sample"))
 {
     string applicationLocation = typeof(App).Assembly.Location;

     key.SetValue("", "URL:" + "CUSTOMPROTOCOL");
     key.SetValue("URL Protocol", "");

     using (var defaultIcon = key.CreateSubKey("DefaultIcon"))
     {
         defaultIcon.SetValue("", applicationLocation + ",1");
     }

     using (var commandKey = key.CreateSubKey(@"shell\open\command"))
     {
         commandKey.SetValue("", "\"" + applicationLocation + "\" \"%1\"");
     }
 }


and i have this code to get the custom protocol value
C#
<pre>static void Main(string[] args)
{
    if(args.Length > 0)
    {
        if (Uri.TryCreate(args[0], UriKind.Absolute, out var uri) &&
            string.Equals(uri.Scheme, UriScheme, StringComparison.OrdinalIgnoreCase))
        {
            // TODO do something with the uri
        }
    }
}


but it can only be used with console,
is there's anyway to get the value inside winform? or can i use program.cs to get the value and insert it into my winform?

What I have tried:

i tried to use program.cs to get the value but i couldn't make a public string to get the value from my winform
Posted
Updated 12-Jun-18 3:56am

1 solution

The form does not have any idea of the existence of the program class.
So, you have to get the argument in the program class, and pass it along to the form when you create it:
C#
class Program
{
   void Main(string[] args) {
      // ...
      Application.Run(new WinForm(args[0]));
   }
}

Of course, this supposes that you also give the WinForm class (or whatever you called it) the corresponding constructor:
C#
class WinForm : Form
{
   // ...
   public WinForm(string arg) {
      // Here you get the first argument which was passed to the <code>Program</code> class in the first place.
   }
}

Hope this helps. Kindly.
 
Share this answer
 
Comments
Hedi Hadi 12-Jun-18 10:43am    
thank you, it works just fine!

i love you xD
phil.o 12-Jun-18 10:48am    
You are welcome.
Thanks for having accepted my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900