用c# 如何实现一个winform 应用程序在一台电脑上只能打开一个
解决方案 »
- 有没有什么类似Queue的东西
- COM与TCP,TCP与COM通讯问题[急]
- 第二次发贴:为什么网站发布后,有些图片看不到了?
- Click事件先判断再触发Html的function 怎么写?
- 窗体有两个panel,如何把用户控件加载到panel2中?
- 如何得到dataGridView中的DataTable的名称
- <ADO.NET程序设计>一书到底有没有附带的练习盘?
- 500分求助:怎么在windows的user control中加一个参数
- 大虾帮忙!用StreamReader读文件,中文显示为乱码,如何解决啊?
- 请问在Windows XP Professional中如何构建无盘工作站??????请介绍几个这方面的网站或资料。
- c#调用MSsql存储过程返回值的问题
- c#在线发邮件错误, 请求各位朋友帮助。先谢谢!
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool ret;
Mutex mutexConfig = new Mutex(true, Application.ProductName, out ret);
if (ret)
{}
else
{
2次启动
}
系统带的
if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) >0)
return true;
else return false; true 说明已经运行了 退出就可以
using System.Collections.Generic;
using System.Windows.Forms;namespace WindowsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (runone())
Application.Run(new Form2());
return 1;
}
private static System.Threading.Mutex mutex;
private static bool runone()
{
bool one;
mutex = new System.Threading.Mutex(true, "WindowsApplication2", out one);
return one;
}
}
}
static class Program
{
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
enum ShowWindowConstants
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1,
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_FORCEMINIMIZE = 11,
SW_MAX = 11
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName); //Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
} //No other instance was found, return null.
return null;
}
public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync(instance.MainWindowHandle, (int)ShowWindowConstants.SW_RESTORE); //Set the real intance to foreground window
SetForegroundWindow(instance.MainWindowHandle);
} /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(true); Process instance = RunningInstance();
if (instance == null)
{
Application.Run(new MainForm());
}
else
{
HandleRunningInstance(instance);
} }
}