如何在winform中获取自定义协议数据 [英] How to get custom protocol data inside winform

查看:94
本文介绍了如何在winform中获取自定义协议数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在winform中获取自定义协议值,



i使用此代码注册密钥

< pre lang =c#> 使用 var key = Registry.CurrentUser.CreateSubKey( SOFTWARE \\Classes \\ + sample))
{
string applicationLocation = typeof运算(应用程序).​​Assembly.Location;

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

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

使用 var commandKey = key.CreateSubKey( @ shell \open \ command))
{
commandKey .SetValue( < span class =code-string> \
+ applicationLocation + \\ %1\);
}
}





我有这个代码来获取自定义协议值

< pre> static  void  Main( string  [] args)
{
if (args.Length > < span class =code-digit> 0 )
{
if (Uri.TryCreate(args [ 0 ],UriKind.Absolute, out var uri)& ;&
string .Equals(uri.Scheme,UriScheme,StringComparison.OrdinalIgnoreCase))
{
// TODO对uri做了一些事情
}
}
}





但它只能用于控制台,

是否有是否在winform中获得价值?或者我可以使用program.cs来获取值并将其插入我的winform?



我尝试过:



i尝试使用program.cs来获取值,但是我无法使用公共字符串从winform获取值

解决方案

表单不知道程序类的存在。

所以,你必须得到程序类中的参数,并在你将其传递给表单时创建它:

  class 程序
{
void Main( string [] args){
// ...
Application.Run( new WinForm(args [ 0 ]));
}
}



当然,这假设您还提供 WinForm 类(或者你称之为)相应的构造函数:

  class  WinForm:Form 
{
// ...
public WinForm( string arg){
// 在这里,您获得了传递给< code> Program< / code>的第一个参数。首先是上课。
}
}



希望这会有所帮助。麻烦。


Hi, i'm trying to get custom protocol value inside my winform,

i used this code to register the keys

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

<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

解决方案

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:

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:

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.


这篇关于如何在winform中获取自定义协议数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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