1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;

namespace ConsoleApplication1 {
    static class Program {
        [STAThread]
        static void Main(string[] args) {
            //Application.EnableVisualStyles();      
            //Application.SetCompatibleTextRenderingDefault(false);   
            //Application.Run(new Form1()); 
            myApplication winAppBase = new myApplication();
            winAppBase.Run(args);
        }
    }

    class myApplication :WindowsFormsApplicationBase {
        public myApplication()
            : base() {
            this.EnableVisualStyles = true;
            this.IsSingleInstance = true;
            this.MainForm = new Form();//スタートアップフォームを設定      
            this.StartupNextInstance += new StartupNextInstanceEventHandler(myApplication_StartupNextInstance);
        }

        void myApplication_StartupNextInstance(object sender, StartupNextInstanceEventArgs e) {
            //ここに二重起動されたときの処理を書く      
            //e.CommandLineでコマンドライン引数を取得出来る
            string cmd = "";
            foreach(string str in e.CommandLine) {
                cmd += str + Environment.NewLine;
            }
            MessageBox.Show(cmd);
        }
    }
}