[急]C#如何检测防止一个程序执行多个副本? 只希望程序在系统中有一个副本存在,如何进行检测????谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ApplicationStart.cs 文件=========================================using System;using System.Diagnostics;using System.Windows.Forms;using System.Reflection;using System.Runtime.InteropServices;using Zhzuo.WinGui;namespace Zhzuo{ /// <summary> /// AppStart 的摘要说明。 /// </summary> public class ApplicationStart { private const int WS_SHOWNORMAL = 1; [DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Process instance = GetRunningInstance(); if(instance == null) { //初始化程序配置信息 //ApplicationSettings.Initialize(); Application.Run(new MainForm()); } else { HandleRunningInstance(instance); } } /// <summary> /// 获取应用程序的实例,没有其它的例程,返回Null /// </summary> /// <returns></returns> public static Process GetRunningInstance() { 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; } return null; } /// <summary> /// 获取窗口句柄 /// </summary> /// <param name="instance"></param> public static void HandleRunningInstance(Process instance) { //确保窗口没有被最小化或最大化 ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL); //设置真实例程为foreground window SetForegroundWindow (instance.MainWindowHandle); } }} 被楼上的兄弟抢了生意,呵呵~看样子我升3角任重道远~~~~~~~==============================================www.wangdiao.com 郁闷,没那么麻烦吧?看看我这个: static void Main() { string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) >0) { MessageBox.Show("系统已经在运行中...","系统警告",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Asterisk); } else Application.Run(new Form1()); } //在Main()函数中#region 仅允许运行一个实例 bool bOnlyOneInstance = false ;Mutex mutex = new Mutex(true, Application.UserAppDataPath.Replace(@"\", "_"), out bOnlyOneInstance) ;if ( !bOnlyOneInstance ){MessageBox.Show("系统已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) ;return ;}//调用主窗体//...#endregion 总结楼上的观点:1、查找进程里有没有相同名称的进程2、用Mutex哈哈,又学到一招阿。 没时间写博客,好久前写的一个,刚好对你有用哈哈,楼上的就是没人用API啊http://dev.csdn.net/develop/article/74/74382.shtm hehe, I know how to use metux, but I do not know how to use api,very good 谁有第四种方法的接着说,哈哈,不要用同样的方法哦1.Mutex2.Process(这里还可以再写几种,不过比我的麻烦的不能算一种了)3.Api4????????????.... 方法就很多了1.内核对象,不一定要mutex,也可以是event,甚至file也可以.2.查找同名的process.3.写注册表,程序启动是查看注册表.4.写文件,程序启动是查看文件. RE:谁有第四种方法的接着说,哈哈,不要用同样的方法哦1.Mutex2.Process(这里还可以再写几种,不过比我的麻烦的不能算一种了)3.Api4????????????....1.设置一个固定文件2.启动时如果不存在该文件则创建,若存在该文件提示已运行并退出3.退出时删除该文件4.必须提供不间断电源防止系统断电(孔已己说的,呵呵) tmfc和 ZhouQiang()说的也是一种方法了,VERY SIMPLE不过也很实用,算是第四种方法了,注册表和文件的方法是一回事,都是作个标识 using "static" have only one copy! 是啊,写文件注册表是不好,万一来个停电,我们不是永远也运行不了程序了,那该死的文件或注册表项在程序结束前要删除的,可没电了,当然删除不了了,以后无法运行了,哈哈看来只有我写的三种方法了,不过也够了吧Love_M65(fanly)的方法怎么用呢?[STAThread] void Main() 还要怎么做?不理解,对进程怎么用static? 国外大牛的ID生成算法!哪位高手帮忙改写成C#代码!谢了。 ASP中dim admin_menu(18,18,15)写法是什么意思 WINFORM里用什么控件可以嵌入WORD,能不能给我提供下参考 windows 界面与代码分离的问题 条形码的打印 ===如何把string型转换为数据库里面的money型=== 谁能给我个时间控件用啊 vs2005里的水晶报表服务器可以发布吗? spring.net管理连接池的管理应用 在窗体上画图形问题 想找个带源代码的(c#)论坛真的好难!找到的都是只有aspx的文件看不到源码呀!!! ReadLine问题请教
=========================================
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;using Zhzuo.WinGui;namespace Zhzuo
{
/// <summary>
/// AppStart 的摘要说明。
/// </summary>
public class ApplicationStart
{
private const int WS_SHOWNORMAL = 1;
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process instance = GetRunningInstance();
if(instance == null)
{
//初始化程序配置信息
//ApplicationSettings.Initialize();
Application.Run(new MainForm());
}
else
{
HandleRunningInstance(instance);
}
}
/// <summary>
/// 获取应用程序的实例,没有其它的例程,返回Null
/// </summary>
/// <returns></returns>
public static Process GetRunningInstance()
{
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;
}
return null;
}
/// <summary>
/// 获取窗口句柄
/// </summary>
/// <param name="instance"></param>
public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//设置真实例程为foreground window
SetForegroundWindow (instance.MainWindowHandle);
}
}
}
www.wangdiao.com
static void Main()
{
string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) >0)
{
MessageBox.Show("系统已经在运行中...","系统警告",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Asterisk);
}
else
Application.Run(new Form1());
}
#region 仅允许运行一个实例 bool bOnlyOneInstance = false ;
Mutex mutex = new Mutex(true, Application.UserAppDataPath.Replace(@"\", "_"), out bOnlyOneInstance) ;if ( !bOnlyOneInstance )
{
MessageBox.Show("系统已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) ;
return ;
}//调用主窗体
//...
#endregion
1、查找进程里有没有相同名称的进程
2、用Mutex
哈哈,又学到一招阿。
哈哈,楼上的就是没人用API啊
http://dev.csdn.net/develop/article/74/74382.shtm
very good
1.Mutex
2.Process(这里还可以再写几种,不过比我的麻烦的不能算一种了)
3.Api
4????????????
....
1.内核对象,不一定要mutex,也可以是event,甚至file也可以.
2.查找同名的process.
3.写注册表,程序启动是查看注册表.
4.写文件,程序启动是查看文件.
谁有第四种方法的接着说,哈哈,不要用同样的方法哦
1.Mutex
2.Process(这里还可以再写几种,不过比我的麻烦的不能算一种了)
3.Api
4????????????
....1.设置一个固定文件
2.启动时如果不存在该文件则创建,若存在该文件提示已运行并退出
3.退出时删除该文件
4.必须提供不间断电源防止系统断电(孔已己说的,呵呵)
不过也很实用,算是第四种方法了,注册表和文件的方法是一回事,都是作个标识
看来只有我写的三种方法了,不过也够了吧
Love_M65(fanly)的方法怎么用呢?
[STAThread]
void Main()
还要怎么做?
不理解,对进程怎么用static?