C#中如何判断一个exe程序已经打开?
展开全部
判断进程中是否存在xxx.exe罗,也就是像任务管理中那样!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//*******************************以下是访问进程信息的静态抽象类。
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Test
{
/// <summary>
/// 确认只有一个程序实例能够运行
/// </summary>
public abstract class OneInstance
{
/// <summary>
/// 用来判断一个指定的程序是否正在运行
/// </summary>
/// <param name="appId">程序名称,长一点比较好,防止有重复</param>
/// <returns>如果程序是第一次运行返回True,否则返回False</returns>
public static bool IsFirst(string appId)
{
bool ret = false;
if (OpenMutex(0x1F0001, 0, appId) == IntPtr.Zero)
{
CreateMutex(IntPtr.Zero, 0, appId);
ret = true;
}
return ret;
}
/// <summary>
/// 尝试调用指定的进程,返回句柄
/// </summary>
/// <param name="dwDesiredAccess">access</param>
/// <param name="bInheritHandle">inheritance option</param>
/// <param name="lpName">object name</param>
/// <returns>相应的句柄</returns>
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr OpenMutex(
uint dwDesiredAccess,
int bInheritHandle,
string lpName
);
/// <summary>
/// 创建进程,返回相应的句柄
/// </summary>
/// <param name="lpMutexAttributes">SD</param>
/// <param name="bInitialOwner">initial owner</param>
/// <param name="lpName">object name</param>
/// <returns>相应的句柄</returns>
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr CreateMutex(
IntPtr lpMutexAttributes,
int bInitialOwner,
string lpName
);
}
}
//**********************以下是调用代码
if (OneInstance.IsFirst("abc.exe"))
{
MessageBox.Show("程序没有启动!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }
else
MessageBox.Show("程序已经启动!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Test
{
/// <summary>
/// 确认只有一个程序实例能够运行
/// </summary>
public abstract class OneInstance
{
/// <summary>
/// 用来判断一个指定的程序是否正在运行
/// </summary>
/// <param name="appId">程序名称,长一点比较好,防止有重复</param>
/// <returns>如果程序是第一次运行返回True,否则返回False</returns>
public static bool IsFirst(string appId)
{
bool ret = false;
if (OpenMutex(0x1F0001, 0, appId) == IntPtr.Zero)
{
CreateMutex(IntPtr.Zero, 0, appId);
ret = true;
}
return ret;
}
/// <summary>
/// 尝试调用指定的进程,返回句柄
/// </summary>
/// <param name="dwDesiredAccess">access</param>
/// <param name="bInheritHandle">inheritance option</param>
/// <param name="lpName">object name</param>
/// <returns>相应的句柄</returns>
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr OpenMutex(
uint dwDesiredAccess,
int bInheritHandle,
string lpName
);
/// <summary>
/// 创建进程,返回相应的句柄
/// </summary>
/// <param name="lpMutexAttributes">SD</param>
/// <param name="bInitialOwner">initial owner</param>
/// <param name="lpName">object name</param>
/// <returns>相应的句柄</returns>
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr CreateMutex(
IntPtr lpMutexAttributes,
int bInitialOwner,
string lpName
);
}
}
//**********************以下是调用代码
if (OneInstance.IsFirst("abc.exe"))
{
MessageBox.Show("程序没有启动!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }
else
MessageBox.Show("程序已经启动!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询