1,程序窗体存在的情况,即窗体跟进程都存在
public const int SW_HIDE = 0;
public const int SW_NORMAL = 1;
public const int SW_SHOWMINIMIZED = 2;
public const int SW_SHOWMAXIMIZED = 3;
[DllImport("User32.dll")]
public static extern void SetForegroundWindow(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
static void Main()
{
Process[] plist = Process.GetProcesses();
int cid = Process.GetCurrentProcess().Id;
string aa = Application.ProductName;
for (int i = 0; i < plist.Length; i++)
{
if (cid != plist[i].Id)
{
string s = plist[i].ProcessName;
if (s == Application.ProductName)
{ ShowWindowAsync(plist[i].MainWindowHandle, SW_NORMAL);
SetForegroundWindow(plist[i].MainWindowHandle);
return;
}
}
Application.Run(new frmPriceAlterClothesDir());
}
2,只存在进程的情况
static void Main()
{
Process[] plist = Process.GetProcesses();
int cid = Process.GetCurrentProcess().Id;
string aa = Application.ProductName;
for (int i = 0; i < plist.Length; i++)
{
if (cid != plist[i].Id)
{
string s = plist[i].ProcessName;
if (s == Application.ProductName)
{
plist[i].Kill();
}
}
}
Application.Run(new frmPriceAlterClothesDir());
}