1: using System;
2: using System.Windows.Forms;
3: using System.IO;
4: using System.Runtime.InteropServices;
5: using Microsoft.VisualBasic.ApplicationServices;
6:
7: namespace YourApplicationNamespace
8: {
9: class SingleInstanceApplication : WindowsFormsApplicationBase
10: {
11: [DllImport("user32.dll")]
12: static extern bool SetForegroundWindow(IntPtr hWnd);
13:
14: public SingleInstanceApplication()
15: {
16: this.IsSingleInstance = true;
17: this.EnableVisualStyles = true;
18: this.Startup += new StartupEventHandler(SingleInstanceApplication_Startup);
19: this.StartupNextInstance += new StartupNextInstanceEventHandler(SingleInstanceApplication_StartupNextInstance);
20: }
21:
22: ~SingleInstanceApplication()
23: {
24: this.Startup -= new StartupEventHandler(SingleInstanceApplication_Startup);
25: this.StartupNextInstance -= new StartupNextInstanceEventHandler(SingleInstanceApplication_StartupNextInstance);
26: }
27:
28: void SingleInstanceApplication_StartupNextInstance(object sender, StartupNextInstanceEventArgs eventArgs)
29: {
30: foreach (string s in eventArgs.CommandLine)
31: {
32: foreach (string ThisFileName in Directory.GetFiles(Path.GetDirectoryName(s), Path.GetFileName(s), SearchOption.TopDirectoryOnly))
33: {
34: ((ManagerMDI)this.MainForm).OpenFile(ThisFileName);
35: }
36: }
37: SetForegroundWindow(this.MainForm.Handle);
38: }
39:
40: void SingleInstanceApplication_Startup(object sender, StartupEventArgs eventArgs)
41: {
42: this.MainForm = new ManagerMDI(null);
43: foreach (string s in eventArgs.CommandLine)
44: {
45: foreach (string ThisFileName in Directory.GetFiles(Path.GetDirectoryName(s), Path.GetFileName(s), SearchOption.TopDirectoryOnly))
46: {
47: ((ManagerMDI)this.MainForm).OpenFile(ThisFileName);
48: }
49: }
50: SetForegroundWindow(this.MainForm.Handle);
51: }
52: }
53:
54: static class Program
55: {
56: /// <summary>
57: /// The main entry point for the application.
58: /// This application is a single-instance application.
59: /// </summary>
60: ///
61: [STAThread]
62: static void Main(string[] args)
63: {
64: Application.SetCompatibleTextRenderingDefault(false);
65: SingleInstanceApplication app = new SingleInstanceApplication();
66: app.Run(args);
67: }
68: }
69: }