如何确保C#的应用程序只被打开一次
http://stackoverflow.com/questions/184084/how-to-force-c-sharp-net-app-to-run-only-one-instance-in-windows
using System.Threading; [DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd); /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "MyApplicationName", out createdNew))
{
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
else
{
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
{
if (process.Id != current.Id)
{
SetForegroundWindow(process.MainWindowHandle);
break;
}
}
}
}
}
上面代码的MyApplicationName需要确保是唯一识别的
VS调试程序的时候
string appName;
appName = Process.GetCurrentProcess().ProcessName;//ZITaker.vshost
appName = Process.GetCurrentProcess().MainModule.FileName;//D:\Software\ZBMYun\SourceCode\ZITaker\ZITaker\bin\Debug\ZITaker.vshost.exe
appName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;//ZITaker
appName = System.Reflection.Assembly.GetExecutingAssembly().Location;//D:\Software\ZBMYun\SourceCode\ZITaker\ZITaker\bin\Debug\ZITaker.exe
直接执行exe的时候
string appName;
appName = Process.GetCurrentProcess().ProcessName;//ZITaker
appName = Process.GetCurrentProcess().MainModule.FileName;//D:\Software\ZBMYun\SourceCode\ZITaker\ZITaker\bin\Debug\ZITaker.exe
appName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;//ZITaker
appName = System.Reflection.Assembly.GetExecutingAssembly().Location;//D:\Software\ZBMYun\SourceCode\ZITaker\ZITaker\bin\Debug\ZITaker.exe
从上面可以看出System.Reflection.Assembly.GetExecutingAssembly().Location适合作为程序的唯一识别码
http://stackoverflow.com/questions/4313756/creating-a-mutex-throws-a-directorynotfoundexception //遇到找不到文件路径的错误
My mutex name had \
in it, which windows was interpreting as a path character. Running:
将路径名中的反斜杠替换成_就可以了
正确的做法:
string appName;
appName = System.Reflection.Assembly.GetExecutingAssembly().Location;
appName = appName.Replace(Path.DirectorySeparatorChar, '_');
using (Mutex mutex = new Mutex(true, appName, out createdNew))
appName = Process.GetCurrentProcess().MainModule.FileName;//D:\Software\ZBMYun\SourceCode\ZITaker\ZITaker\bin\Debug\ZITaker.vshost.exe
ZBM.ZITaker.Log.OperationLog.Instance.WriteLog(appName, ZBM.ZITaker.Log.LogType.UI);
appName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;//ZITaker
ZBM.ZITaker.Log.OperationLog.Instance.WriteLog(appName, ZBM.ZITaker.Log.LogType.UI);
appName = System.Reflection.Assembly.GetExecutingAssembly().Location;//D:\Software\ZBMYun\SourceCode\ZITaker\ZITaker\bin\Debug\ZITaker.exe
ZBM.ZITaker.Log.OperationLog.Instance.WriteLog(appName, ZBM.ZITaker.Log.LogType.UI);
最终版本
using System;
using System.Windows.Forms;//Application
using System.Diagnostics;//Process
using System.Threading;//Mutex
using System.Runtime.InteropServices;//DllImport
using System.IO;//Path static class Program
{
[DllImport("User32.dll")]
//This function puts the thread that created the specified window into the foreground and activates the window.
private static extern bool SetForegroundWindow(IntPtr hWnd); /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
try
{
bool createdNew;
string appName;
appName = System.Reflection.Assembly.GetExecutingAssembly().Location;
appName = appName.Replace(Path.DirectorySeparatorChar, '_');using (Mutex mutex = new Mutex(true, appName, out createdNew))
{
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main());
}
else
{
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
{
if (process.Id != current.Id)
{
SetForegroundWindow(process.MainWindowHandle);
break;
}
}
}
}
}
catch (Exception ex)
{ }
}
}
http://www.cnblogs.com/xiashengwang/archive/2012/08/29/2662366.html
这篇文章中也提到了Mutex实现一次只打开一个应用程序
如何确保C#的应用程序只被打开一次的更多相关文章
- 限制pyqt5应用程序 只允许打开一次
起因 pyqt5程序创建桌面快捷方式后,多次单击图标 会打开多个UI界面,这种情况肯定是不允许的! 解决 if __name__ == '__main__': try: app = QtWidgets ...
- Delphi实现程序只运行一次并激活已打开的程序
我们的程序有时候只允许运行一次,并且最好的情况是,如果程序第二次运行,就激活原来的程序.网上有很多的方法实现程序只运行一次,但对于激活原来的窗口却都不怎么好.关键就在于激活原来的程序,一般的做法是在工 ...
- 【Android UI设计与开发】第05期:引导界面(五)实现应用程序只启动一次引导界面
[Android UI设计与开发]第05期:引导界面(五)实现应用程序只启动一次引导界面 jingqing 发表于 2013-7-11 14:42:02 浏览(229501) 这篇文章算是对整个引导界 ...
- Linux守护进程实现程序只运行一次
1.守护进程 守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件. 2.让程序只运行一次 如果让程序只运行一次,有很多方法,此处的一种 ...
- 【Android UI设计与开发】3.引导界面(三)实现应用程序只启动一次引导界面
大部分的引导界面基本上都是千篇一律的,只要熟练掌握了一个,基本上也就没什么好说的了,要想实现应用程序只启动一次引导界面这样的效果,只要使用SharedPreferences类,就会让程序变的非常简单, ...
- WPF 只允许打开一个实例
我们有时候只希望我们的程序只打开一个实例,也就是我们的软件只有一次被打开. 那么我们可以通过一个办法知道,在这个软件打开前是不是打开过一个,还没关闭.也就是是否存在另一个程序在运行. 下面是一个简单方 ...
- [转]使用互斥对象让程序只运行一次(delphi)
使用互斥对象让程序只运行一次“怎么让我的程序在运行时不能重复打开?”经常在论坛上看到有朋友问这方面的问题.本文将比较详细的说明这一问题,并给出一个较为完善的解决方案. 尽管这已经不是一个新问题了,但这 ...
- [VC]在VC++中实现让程序只运行一个实例的方法且实现该实例
方法一: 有时候在开发应用程序时,希望控制程序运行唯一的实例.例如,最常用的mp3播放软 件Winamp,由于它需要独占计算机中的音频设备,因此该程序只允许自身运行唯一的一个例程.在Visual C+ ...
- 小程序 webview 自动打开新页面
小程序 webview 自动打开新页面 iframe 效果 https://nervjs.github.io/taro/docs/components/open/web-view.html 怎么阻止小 ...
随机推荐
- make_ext4fs 失败
root@fengyun-server:/home/fmake_ext4fsengyun/android/reverse_engineer/rom制作# ./make_ext4fs -l 700M - ...
- csv的文件excel打开长数字后面位变0的解决方法
对于有大数字的CSV文件,应使用导入,而不是打开.这里以Excel2010为例,其它版本也可以参照: 打开Excel,此时Excel内为空白文档 点击工具栏中的[数据]→[自文本] 在“导入文本文件” ...
- CF#301 B:School Marks(贪心)
B:School Marks 有n个测试,已经完成了k个,每个测试得分为a[i],接下来的分数不知道,让我们求出任何一个满足题意的即可,满足题意就是n个测试的得分总和<=x, 中位数>=y ...
- python发送邮件的2种方式
发送邮件的2种方式1.匿名发送 smtpObj = smtplib.SMTP(host, port) smtpObj.sendmail(from_addr, to_addrs, message.as_ ...
- CloudFoundry V2 单机版离线安装(伪离线安装)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wangdk789/article/details/30255763 之前安装CloudFou ...
- linux环境下的python安装过程
一.下载python源码包 打开ubuntu下的shell终端,通过wget命令下载python源码包,如下图所示: wget https://www.python.org/ftp/python/3. ...
- Spark Streaming Checkpoint反序列化问题分析
转载自:https://mp.weixin.qq.com/s/EQgDUSf3TK0oVg1xmg-49Q Checkpoint是Spark Streaming中的核心机制,它为应用程序的7*24小时 ...
- Docker给运行中的容器添加映射端口
方法一: 1.获得容器IP将container_name 换成实际环境中的容器名docker inspect `container_name` | grep IPAddress 2. iptables ...
- jQuery实现复选框 全选、反选、全不选
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- #pragma 的用法
它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作.#pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的情况下,给出主机或操作系统专有的特征.依据定义,编译指示是机 ...