How to activate minimized Application using C#.Net?

2024-02-09T09:20:15.0833333+00:00

Main application can minimized or other application can Maximized or popup. So in that case i want to maximize or activate my main application through C#.Net code.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,648 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 29,106 Reputation points Microsoft Vendor
    2024-02-09T09:44:26.13+00:00

    Hi @Thanga Perumal Ramasamy (SX/ETM12-EM)

    You can use the ShowWindowAsync function and the SetForegroundWindow function to activate and bring your app window to the foreground.

        [DllImport("user32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
    
        static void Main(string[] args)
        {
            string processName = "YourApplicationName";
    
            Process[] processes = Process.GetProcessesByName(processName);
    
            if (processes.Length > 0)
            {
                IntPtr mainWindowHandle = processes[0].MainWindowHandle;
    
                ShowWindowAsync(mainWindowHandle, 3);
    
                SetForegroundWindow(mainWindowHandle);
            }
            else
            {
                Console.WriteLine("Application not found.");
            }
        }
    }
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments