让程序只能运行一次

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

namespace frameTest
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            string MName = Process.GetCurrentProcess().MainModule.ModuleName;
            string PName = Path.GetFileNameWithoutExtension(MName);
            Process[] myProccess = Process.GetProcessesByName(PName);
            if (myProccess.Length > 1)
            {
                MessageBox.Show("程序已经运行,不能重复打开", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {


                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
}

posted @ 2013-09-29 15:58  美丽的矩阵  阅读(140)  评论(0编辑  收藏  举报