2015-10-15 1 views
2

Cette question concerne une application WPF. Je voudrais ouvrir des fichiers associés avec mon application personnalisée. J'ai trouvé le moyen d'ouvrir les fichiers associés. mais encore, je ne sais pas comment gérer l'application dupliquée.ouvrir les fichiers associés comme «bloc-notes ++ fonctionnalités de l'onglet» dans WPF

Par exemple, si je clique sur 'a.txt2', mon application l'ouvre. Et je clique sur 'a2.txt2', il ouvre également un autre processus de mon application.

Je veux obtenir des informations de deuxième clic uniquement dans la première application, pas l'exécution de mon application lorsque l'on clique sur plusieurs fichiers associés. Il est similaire au fichier ouvert 'notepad ++' pour les fonctions d'onglet.

Voici mes codes.

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

<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); 
    } 
} 

Merci.

Répondre

1

utilisation Technic unique instance singleton fond qui est disponible avec espace de noms "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]); 
     } 



    } 
} 
+0

Great !! Merci beaucoup. Il fonctionne très bien. Votre code est simple et facile à comprendre !! Merci pour votre aide. – kimtk

+0

vous êtes les bienvenus! – zamoldar