永远只打开一个应用程序

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;

namespace Phone2007
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            int i = 0;
            Process current = Process.GetCurrentProcess();
            Process[] processName = Process.GetProcessesByName(current.ProcessName);
            foreach (Process ps in processName)
            {
                if (current.Id != ps.Id)
                {
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
                    {
                        i++;
                    }
                }
            }
            if (i > 0)
            {
                return;
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
          
        }
    }

posted on 2008-05-15 16:36  leanco  阅读(93)  评论(0)    收藏  举报

导航