C# Single Instance WinForms and Microsoft.VisualBasic.dll

引用自:http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicrosoftVisualBasicdll.aspx
 
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
 
namespace SuperSingleInstance
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            string[] args = Environment.GetCommandLineArgs();
            SingleInstanceController controller = new SingleInstanceController();
            controller.Run(args);
        }
    }
 
    public class SingleInstanceController : WindowsFormsApplicationBase
    {
        public SingleInstanceController()
        {
            IsSingleInstance = true;
 
            StartupNextInstance += this_StartupNextInstance;
        }
 
        void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
        {
            Form1 form = MainForm as Form1; //My derived form type
            form.LoadFile(e.CommandLine[1]);
        }
 
        protected override void OnCreateMainForm()
        {
            MainForm = new Form1();
        }
    }
}
posted @ 2012-04-25 22:17  谷安  阅读(366)  评论(0)    收藏  举报