在WPF中打开相关文件,例如“记事本++选项卡功能" [英] open associated files like 'notepad ++ tab features' in WPF

查看:120
本文介绍了在WPF中打开相关文件,例如“记事本++选项卡功能"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与WPF应用程序有关. 我想用我的自定义应用程序打开一些关联的文件. 我找到了打开关联文件的方法. 但是,我仍然不知道如何处理重复的应用程序.

This question is about a WPF application. I would like to open some associated files with my custom application. I found the way to open associated files. but still, I don't know how to handle dupplicated application.

例如, 如果我单击"a.txt2",则我的应用程序将其打开. 然后单击"a2.txt2",它还会打开我的应用程序的另一个进程.

For example, if I click 'a.txt2', my app opens it. And I click 'a2.txt2', it also opens another process of my app.

我只想在第一个应用程序中获得第二次单击信息,而不是在多次单击相关文件时获得我的应用程序的执行信息. 类似于记事本++"打开文件到标签页功能.

I want to get second click info only in the first app, not the execution of my app when associated files are clicked many. It's similar to 'notepad ++' open file to tab features.

这是我的密码.

1 MainWindow.cs

1 MainWindow.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.Loaded += new RoutedEventHandler(MainContainer_Loaded);
    }
    void MainContainer_Loaded(object sender, RoutedEventArgs e)
    {
        string fn = "x";
        if (Application.Current.Properties["ArbitraryArgName"] != null)
        {
            fn = Application.Current.Properties["ArbitraryArgName"].ToString();

        }
        this.Title = fn;
    }
}

2个app.xaml

2 app.xaml

<Application x:Class="sample.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         >
<Application.Resources>

</Application.Resources>
<!--
StartupUri="MainWindow.xaml"
Startup="App_Startup"--></Application>

3个app.cs

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        // Check if this was launched by double-clicking a doc. If so, use that as the
        // startup file name.
        string fname = "No filename given";
        if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null
        && AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Length > 0)
        {

            try
            {
                fname = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0];

                // It comes in as a URI; this helps to convert it to a path.
                Uri uri = new Uri(fname);
                fname = uri.LocalPath;

                this.Properties["ArbitraryArgName"] = fname;
            }
            catch
            {
                // For some reason, this couldn't be read as a URI.
                // Do what you must...
            }
        }

        string procName = Process.GetCurrentProcess().ProcessName;

        // get the list of all processes by the "procName"       
        Process[] processes = Process.GetProcessesByName(procName);
        if (processes.Length > 1)
        {

            MessageBox.Show(procName + " already running");

            //App a = new App();
            //a.Shutdown();
        }
        else
        {
        }
        MainWindow mainWindow = new MainWindow();
        mainWindow.Show();
        base.OnStartup(e);
    }
}

谢谢.

推荐答案

使用可用于"Microsoft.VisualBasic"命名空间的底部单例实例技术:

using System;

namespace Sample
{
    public class Startup
    {
        [STAThread]
        public static void Main(string[] args)
        {
            SingleInstanceApplicationWrapper wrapper = new SingleInstanceApplicationWrapper();
            wrapper.Run(args);
        }
    }

    public class SingleInstanceApplicationWrapper :
        Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
    {
        public SingleInstanceApplicationWrapper()
        {
            // Enable single-instance mode.
            this.IsSingleInstance = true;
        }

        // Create the WPF application class.
        private WpfApp app;
        protected override bool OnStartup(
            Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
        {


            app = new WpfApp();
            app.Run();

            return false;
        }

        // Direct multiple instances
        protected override void OnStartupNextInstance(
            Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs e)
        {
            if (e.CommandLine.Count > 0)
            {
                ((MainWindow)app.MainWindow).openFile(e.CommandLine[0]);
            }
        }
    }

    public class WpfApp : System.Windows.Application
    {
        protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
            base.OnStartup(e);

            // Load the main window.
            MainWindow main = new MainWindow();
            this.MainWindow = main;
            main.Show();

            // Load the document that was specified as an argument.
            if (e.Args.Length > 0) main.openFile(e.Args[0]);
        }



    }   
}

这篇关于在WPF中打开相关文件,例如“记事本++选项卡功能"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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