皮尔洛的花园  
开发小积累,生活小花絮的集合

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;

namespace AvsRecoder
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Process instance = RunningInstance();
            if (instance == null)
            {
                //如果没有其它例程,就新建一个窗体   
                Application.Run(new mainForm());
            }
            else
            {
                MessageBox.Show("程序已在运行");
            }
        }

        public static Process RunningInstance()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);

            //遍历正在有相同名字运行的例程   
            foreach (Process process in processes)
            {
                //忽略现有的例程   
                if (process.Id != current.Id)
                {
                    //确保例程从EXE文件运行   
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
                        current.MainModule.FileName)
                    {
                        //返回另一个例程实例   
                        return process;
                    }
                }
            }

            //没有其它的例程,返回Null   
            return null;
        }
    }  // end class
}   // end NameSpace

posted on 2010-01-18 20:45  皮尔洛的花园  阅读(397)  评论(0)    收藏  举报