创建帮助类
单例帮助类
public class MainHelper
{
public static Process RunningInstance(string location)
{
var current = Process.GetCurrentProcess();
var processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
if (process.Id != current.Id)
{
if (location.Replace("/", @"\") == current.MainModule.FileName)
{
return process;
}
}
}
return null;
}
public static void Active(Process instance)
{
ShowWindowAsync(instance.MainWindowHandle, 1);
SetForegroundWindow(instance.MainWindowHandle);
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
}
// ShowWindowAsync 说明
private const int SW_HIDE = 0;
private const int SW_NORMAL = 1;
private const int SW_MAXIMIZE = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_SHOW = 5;
private const int SW_MINIMIZE = 6;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
调用
var location = Assembly.GetExecutingAssembly().Location;
var instance = MainHelper.RunningInstance(location);
if (instance != null)
{
MainHelper.Active(instance);
}
else
{
Application.Run(new PhysicalUploadForm());
}
标签:Process,const,int,SW,private,instance,实例,运行,Winform From: https://www.cnblogs.com/spy0505/p/17140732.html