C#只允许运行一个实例

var preInstance = Process.GetProcesses().FirstOrDefault(p =>
            {
                try
                {
                    if (p.Id != Process.GetCurrentProcess().Id
                        && p.MainModule != null
                        && p.MainModule.FileName.ToLower() == Assembly.GetExecutingAssembly().Location.ToLower())
                        return true;

                    return false;
                }
                catch
                {
                    return false;
                }
            });

            if (preInstance != null)
            {
                MessageBox.Show("程序已在运行!");
                return;
            }
原文地址:https://www.cnblogs.com/nanfei/p/14009285.html