简体   繁体   English

将应用程序注册到URI方案

[英]Registering an application to a URI Scheme

I have a wpf app I have registered as a URI Scheme by doing the following. 我有一个wpf应用程序,我通过执行以下操作注册为URI方案。

HKEY_CLASSES_ROOT
-->myappname
   -->shell
      -->open
         -->command
            (Default) = "c:\pathtomyapp\app.exe"

Fantastic! 太棒了! However, my application enforces that only one instance can run at a time. 但是,我的应用程序强制一次只能运行一个实例。 How can I detect that my app is already running and for example bring it to the foreground? 如何检测我的应用程序是否已在运行,例如将其带到前台?

You can use a named mutex to detect that application is already running. 您可以使用命名的互斥锁来检测该应用程序是否已在运行。 Or, if you have a GUI app, you can inherit your form from VisualBasic's SingleInstance application , and it will do routhgly the same for you. 或者,如果你有一个GUI应用程序,你可以从VisualBasic的SingleInstance应用程序继承你的表单,它会为你做同样的事情。

  public class SingleInstanceController
    : WindowsFormsApplicationBase
  {
    public SingleInstanceController()
    {
      // Set whether the application is single instance
      this.IsSingleInstance = true;

      this.StartupNextInstance += new
        StartupNextInstanceEventHandler(this_StartupNextInstance);
    }

    void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
    {
      // Here you get the control when any other instance is
      // invoked apart from the first one.
      // You have args here in e.CommandLine.

      // You custom code which should be run on other instances
    }

    protected override void OnCreateMainForm()
    {
      // Instantiate your main application form
      this.MainForm = new Form1();
    }
  }

[STAThread]
static void Main(string[] args)
{    
  SingleInstanceController controller = new SingleInstanceController();
  controller.Run(args);
}

It does not matter whenever you write your code in C#, as this class is avaliable as a part of .Net framework and for all languages. 无论何时用C#编写代码都没关系,因为这个类可以作为.Net框架和所有语言的一部分。

And here is a wrapper for the WPF 这是WPF包装器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM