如何在C#中检测一个windows应用程序是否已经启动?如果已经启动就不让该程序再次启动?急!!!谢谢各位大哥 使用hook,检查有没有同名的进程就可以了,可以看看这个:http://www.codeproject.com/csharp/GlobalSystemHook.asp 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Process curProcess = Process.GetProcessesByName(strProgramName);if (curProcess.Length > 1 )... namespace system.dignostics...楼上的对阿。 public static Process RunningInstance() { try { 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; } } } } catch {// MessageBox.Show ("非法操作,请检查您系统的线程计数器配置!", "警告",MessageBoxButtons.OK, MessageBoxIcon.Warning);// Application.Exit(); return null; } return null; } Process pro = Process.GetProcessesByName(strProgramName);if ( pro.Responding ){ //不启动服务// } 用API中FINDWONDOWS[DllImport("user32.dll", EntryPoint="FindWindow")]public static extern int FindWindow ( string lpClassName, string lpWindowName);lpClassName ---- String,指向包含了窗口类名的空中止(C语言)字串的指针;或设为零,表示接收任何类 lpWindowName --- String,指向包含了窗口文本(或标签)的空中止(C语言)字串的指针;或设为零,表示接收任何窗口标题 用 liulxmooo(娃娃) 的行 一个更简单的方法!public static void Main(string[] args) { //声明互斥体。 System.Threading.Mutex mutex = new System.Threading.Mutex(false, "ThisShouldOnlyRunOnce"); //判断互斥体是否使用中。 bool Running = !mutex.WaitOne(0, false); if (! Running) Application.Run(new FormMain()); else MessageBox.Show("应用程序已经启动!"); } ApplicationStart.cs 文件=========================================using System;using System.Diagnostics;using System.Windows.Forms;using System.Reflection;using System.Runtime.InteropServices;using ZZ.WinGui;namespace ZZ{ /// <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); } }} System.Timers.Timer停不下来? .net 的find和自己写的有什么不同 请教一个数据传输的问题 checkboxlist 如何实现单选 利用内存(Api)转换数据 如何降低图片的内存占用量 C#如何在数据库把题目选出显示出来 DataGridView如何实现增删改查?谢谢~~~~~~~~~ c#怎样检索数据库纪录? 我在一个WINFORM中实例化一个类,并调用类中的一个方法,此方法返回一个DATASET,不过好象没有执行!为何? 水晶报表部署问题!? 水晶报表疑难问题(相信很多人都会碰到)
if (curProcess.Length > 1 )
...
楼上的对阿。
{
try
{
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;
}
}
}
}
catch
{
// MessageBox.Show ("非法操作,请检查您系统的线程计数器配置!", "警告",MessageBoxButtons.OK, MessageBoxIcon.Warning);
// Application.Exit();
return null;
}
return null; }
if ( pro.Responding )
{ //不启动服务// }
[DllImport("user32.dll", EntryPoint="FindWindow")]
public static extern int FindWindow (
string lpClassName,
string lpWindowName
);
lpClassName ---- String,指向包含了窗口类名的空中止(C语言)字串的指针;或设为零,表示接收任何类 lpWindowName --- String,指向包含了窗口文本(或标签)的空中止(C语言)字串的指针;或设为零,表示接收任何窗口标题
public static void Main(string[] args)
{
//声明互斥体。
System.Threading.Mutex mutex = new System.Threading.Mutex(false, "ThisShouldOnlyRunOnce");
//判断互斥体是否使用中。
bool Running = !mutex.WaitOne(0, false);
if (! Running)
Application.Run(new FormMain());
else
MessageBox.Show("应用程序已经启动!");
}
=========================================
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;using ZZ.WinGui;namespace ZZ
{
/// <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);
}
}
}