如何确保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 怎么阻止小 ...
随机推荐
- Windows的445端口(文件共享)
周鸿祎:教育网大量电脑445端口暴露,导致中招_科技_腾讯网 http://tech.qq.com/a/20170513/016133.htm 互联网周鸿祎2017-05-13 12:04 据36 ...
- spring data jpa 遇到的问题
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.O ...
- Android项目使用Eclipse进行单元测试
Android项目如果每次都整个调试的话,要加载UI,会等很长时间.所以单元测试就显得很方便了. 要进行单元测试,首先得修改下AndroidManifest.xml文件.在Instrument标签里点 ...
- python学习笔记(十七)网络编程之urllib模块
如何用python打开一个网站或者请求一个接口呢,我们在这篇博客介绍一下. 首先我们得导入一个urllib模块,这个模块是python自带的标准模块,直接导入就能使用,但是用起来不方便,先看个简单的打 ...
- IntBuffer类的基本用法
package com.ietree.basicskill.socket.basic.nio; import java.nio.IntBuffer; /** * Created by Administ ...
- 关于Windows Service的一个编写技巧
写过Windows Service的朋友都知道服务是不可以直接在vs里面启动调试,我们必须修改Program.cs文件来达到我们调试的目的,等服务调试好了以后还要把代码改回来,显非常的不方便,在这里为 ...
- 查看Oracle相关日志 ADRCI
ADRCI 进去以后 show home
- javascript笔记——js获取input标签中光标的索引
出处:http://www.cnblogs.com/MrZouJian/p/5850553.html function getTxt1CursorPosition(){ var oTxt1 = doc ...
- Ajax跨域请求 同源策略与Jsonp
同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础之上的 ...
- v9上传图片/附件失败出现undefined的解决方法之一
把phpcms\modules\attachment\attachments.php中将 if(empty($this->userid)){改成 ...