static void Main()
{
//获取当前进程
Process currentProcess = Process.GetCurrentProcess();
//获取当前运行程序完全限定名
string currentFileName = currentProcess.MainModule.FileName;
//获取进程名为ProcessName的Process数组。
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
//遍历有相同进程名称正在运行的进程
foreach (Process process in processes)
{
if (process.MainModule.FileName == currentFileName)
{
if (process.Id != currentProcess.Id) //根据进程ID排除当前进程
{
//MessageBox.Show("系统已经运行!", "系统提示");
return;//返回已运行的进程实例
}
}
}我做的程序被我隐藏到系统栏,我想就跟TT 浏览器的功能一样,隐藏之后如果忘记了,再次运行程序就激活程序。
可是 在 //MessageBox.Show("系统已经运行!", "系统提示");
下面我不知道怎么写了,请给出代码。(再问一下我这种方法检查实例好吗?)
{
//获取当前进程
Process currentProcess = Process.GetCurrentProcess();
//获取当前运行程序完全限定名
string currentFileName = currentProcess.MainModule.FileName;
//获取进程名为ProcessName的Process数组。
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
//遍历有相同进程名称正在运行的进程
foreach (Process process in processes)
{
if (process.MainModule.FileName == currentFileName)
{
if (process.Id != currentProcess.Id) //根据进程ID排除当前进程
{
//MessageBox.Show("系统已经运行!", "系统提示");
return;//返回已运行的进程实例
}
}
}我做的程序被我隐藏到系统栏,我想就跟TT 浏览器的功能一样,隐藏之后如果忘记了,再次运行程序就激活程序。
可是 在 //MessageBox.Show("系统已经运行!", "系统提示");
下面我不知道怎么写了,请给出代码。(再问一下我这种方法检查实例好吗?)
你可以通过SendMessageBox给进程发送消息. 但你的程序需要自己处理消息.另外你可以使用_Process.MainWindowHandle; 这个是窗体句柄.通过API来设置窗 [DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public static extern bool SetForegroundWindow(IntPtr hWnd); 我在上面放的声明 然后在//MessageBox.Show("系统已经运行!", "系统提示");
下面 打的
SetForegroundWindow(currentProcess.MainWindowHandle);
但是不好用啊,程序没反应
Wnd := FindWindow(nil, 'aa');
ShowWindow(Wnd, SW_SHOW);
ShowWindow(Wnd, SW_RESTORE);
SetForegroundWindow(Wnd);
很多 API 函数声明声明的
谢谢了
基于.Net平台应用程序唯一运行实例实现
http://blog.csdn.net/zhzuo/archive/2006/06/30/857405.aspx
http://blog.csdn.net/zhzuo/archive/2006/07/04/874745.aspx