在你的程序启动的代码中加入如下的的代码就可以了:
System.Diagnostics.Process[] pros=System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
if (pros.Length>1)
{
Application.Exit();
return;
}
System.Diagnostics.Process[] pros=System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
if (pros.Length>1)
{
Application.Exit();
return;
}
static void Main()
{
try
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\PerfProc\Performance", true);
if (rk!=null)
{
object tmpObj=rk.GetValue("Disable Performance Counters");
if (tmpObj!=null && !tmpObj.Equals(0))
{
rk.SetValue("Disable Performance Counters",0);
//要添加一个日志表明此处删除了一个注册表值
}
}
System.Diagnostics.Process[] pros=System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
if (pros.Length>1)
{
Application.Exit();
return;
}
Application.Run(new frmMainServer());
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}
Mutex m=new Mutex(true,"test",out createdNew);
if (createdNew)
{
Application.Run(new Form());
m.ReleaseMutex();
}
else
{
MessageBox.Show("本程序只允许同时运行一个");
}
尽点力
下面是肯定可以的:方法:改写程序启动的函数(main())。
具体方法:
[STAThread]
static void Main()
{
int ProceedingCount = 0;
Process[] ProceddingCon = Process.GetProcesses();
foreach(Process IsProcedding in ProceddingCon)
{
if(IsProcedding.ProcessName == Process.GetCurrentProcess().ProcessName)
{
ProceedingCount += 1;
}
}
if(ProceedingCount > 1)
{
MessageBox.Show("该系统已经在运行中。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
Application.EnableVisualStyles();
Application.DoEvents();
Application.Run(new [本系统的主界面]());
}
}
static void Main() {
Process[] myProcesses = Process.GetProcessesByName(Application.ProductName );
if(myProcesses.Length<=1)
Application.Run(new LogOn());
else
MessageBox.Show("程序已经启动!","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
Application.Exit();
}
精简版C#不支持 System.Diagnostics.Processto weilysunhg(一天到晚红烧的鱼) :
精简版C#不支持 Mutex m=new Mutex(true,"test",out createdNew);
Mutex仅仅支持 new Mutex() 和 new Mutex(bool);to hbxtlhx(下着春雨的天):
精简版C#不支持 RegistryKey.
老兄,Pocket PC 2003是微软提供的PDA的一种基于Windows CE.net的操作系统,里面的ROM内置了 .net framework精简版,不能使用完整版。不是我不想用完整版,而是环境不允许,受到太多的限制。
public class FileMutex
{
private FileStream aFile;
public FileMutex()
{
}
public bool Open()
{
string strAppDir=Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
string strFile=strAppDir+@"\file.dat"; try
{
aFile=new FileStream(strFile, FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.None);
}
catch
{
return false;
} return true;
}
public void Dispose()
{
try
{
aFile.Close();
aFile=null;
}
catch
{
}
}
}//使用类 FileMutex
static void Main()
{
FileMutex f=new FileMutex();
if(f.Open())
Application.Run(new MainFrm());
f.Dispose();
}可以有效解决问题。