SoFunction
Updated on 2025-03-06

C# Method to determine whether a program is running

This article describes the method of C# to determine whether a program is running, and is shared with you for your reference.

The specific implementation method is as follows:

[DllImport("")]
private static extern bool
SetForegroundWindow(IntPtr hWnd);
[DllImport("")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("")]
private static extern bool IsIconic(IntPtr hWnd);
// Message function[DllImport("", EntryPoint = "PostMessageA")]
public static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

[DllImport("")]
public static extern IntPtr FindWindow(string strclassName, string strWindowName);
[DllImportAttribute("")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MAXIMIZE = 0xF030;

private string exeName = "SaoMiaoApp";
public void SetForm()
{
  Process[] processes = (exeName);

  if ( > 0)
  {
 IntPtr hWnd = processes[0].MainWindowHandle;

 if (IsIconic(hWnd))
   ShowWindowAsync(hWnd, 9);// 9 is the SW_RESTORE flag, indicating the restoration of the form //SendMessage(hWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
 SetForegroundWindow(hWnd);
  }
  else
  {
 (exeName + ".exe");
  }
}

I hope this article will be helpful to everyone's C# programming.