【问题标题】:Prevent file association from opening the app again防止文件关联再次打开应用
【发布时间】:2016-10-03 16:35:20
【问题描述】:

我开发了一个 wpf 应用程序并关联了一个名为 .fcsc 的文件类型。双击文件时应用程序打开,但它执行应用程序的新实例。我想要的是,如果应用程序已经在运行,请在该实例中打开文件,而不是在新实例中。

我该如何存档?

这是我打开文件时所拥有的:

if (AppDomain.CurrentDomain.SetupInformation != null 
                && AppDomain.CurrentDomain.SetupInformation.ActivationArguments != null 
                && AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null &&
                AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Any())
            {
                // is a file association invoke, open the window
                InstallPluginWindow installPluginWindows = new InstallPluginWindow(AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData);
                installPluginWindows.Show();
                installPluginWindows.Owner = this;
                this.Opacity = 0.5;
                this.IsEnabled = false;
                installPluginWindows.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
                installPluginWindows.Closed += installPluginWindows_Closed;
            }
            else
            {
                NegotiateLogin();
            }

【问题讨论】:

    标签: c# .net wpf windows file-association


    【解决方案1】:

    简单的部分是设置互斥体来检查另一个实例。在您的应用程序主代码或启动代码中,您可能需要如下代码:

        bool exclusive;
        System.Threading.Mutex appMutex = new System.Threading.Mutex(true, "MyAppName", out exclusive);
        if (!exclusive)
        {
          //Another instance running
        }    ...
        GC.KeepAlive(appMutex);
    

    接下来,您需要实现一种方法来向第一个应用程序实例发送消息并传入被双击的文件名。您可以通过多种方式执行此操作,但是,向主窗口发送自定义消息似乎是最直接的。 Here 是向另一个应用程序发送消息的替代方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多