方法1.按如下代码更改
方法2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
namespace WindowsFormsApplicationTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//防止启动多个应用和序
if (RunningInstance() != null)
{
System.Windows.Forms.MessageBox.Show("已经有一个程序在运行");
return;
}
Application.Run(new Form1());
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
namespace WindowsFormsApplicationTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//防止启动多个应用和序
if (RunningInstance() != null)
{
System.Windows.Forms.MessageBox.Show("已经有一个程序在运行");
return;
}
Application.Run(new Form1());
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
}
}
方法2
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace TestForm.TreeViewAndMDIForm
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
bool createdNew;
Mutex m = new Mutex(true, "test", out createdNew);
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
m.ReleaseMutex();
}
else
{
MessageBox.Show("系统只允许运行一个实例!", "系统提示");
}
}
}
}
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace TestForm.TreeViewAndMDIForm
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
bool createdNew;
Mutex m = new Mutex(true, "test", out createdNew);
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
m.ReleaseMutex();
}
else
{
MessageBox.Show("系统只允许运行一个实例!", "系统提示");
}
}
}
}