[topic] 二重起動の禁止とコマンドライン引数の取得

マルチタスクな環境でアプリケーションの二重起動を禁止し、
後から起動されようとした実行ファイルに渡されたコマンドライン引数を取得、表示して下さい。

.NET Framework上のWindowsアプリケーションではこんな方法があります。

VBのWindowsFormsApplicationBaseクラスを利用するので
Microsoft.VisualBasicを参照に加えます。
 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);
        }
    }
}

Posted feedbacks

Number of comments:1 Nested Flatten
  1. 1 Other C#

Index

Feed

Other

Link

Pathtraq

loading...