Start a Process in the Foreground in C# .NET without AppActivate

In my particular scenario, I was using System.Diagnostics.Process to start another application, compiled separately, which communicated with the original app which launched the process and used Windows inter-process communication (IPC) to share information across applications using my own protocol.

I won’t delve into the specifics of the above setup, but I did find it interesting that what seemed so simple to do in VB .Net (migrating an old app to C#) was potentially much more complex in C#.

(In VB I was using the function AppActivate).

So my initial Google search for “process.start foreground” led me to a codeproject site that described Process.Start and then 4 links down I discovered an experts exchange link which helped me conceptually reword my search to “bring window into foreground” and led to the discovery on MSDN of the SetForegroundWindow Function.

Reading this definitely started to bring back some old memories of COM and Win32 API development, so I was sure I was beginning to overcomplicated things and went back to my searches to see if I could replace the way I was previously using in VB.

I started searching for “AppActivate alternatives in C#”, but this too was to no avail, as most places seemed to simply suggest something like:

(add reference to Microsoft.Visualbasic first)

using Microsoft.VisualBasic;

void Main() {

Technically the above does work, but caused a strange “Process 5212′ was not found” error on exit of the second application.

So I dusted off the old C++ Win32 API notes from storage, cracked open a redbull and set to work. To my surprise, the end result was actually very simple. See below:

Basic code to SetForeGroundWindow(don’t forget to add interop reference)

using System.Runtime.InteropServices;

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern bool SetForegroundWindow(IntPtr hwnd);

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); 

void Main() {
IntPtr splashwindow = FindWindowByCaption(IntPtr.Zero, "Title of Form");

You might be tempted to use something like MainWindowHandle of Diagnostics.GetCurrentProcess, but be advised this does not always return the correct Window handle you are looking for.

See articles related articles for any questions or issues on:

Inter-process communication,
Code Project
MSDN (SetForegroundWindow Function),
MSDN (AppActivate),
MSDN (Process.GetCurrentProcess), (AppActivate),
Vaibhav Gaikwad (SetForeGroundWindow),
VBForums (AppActivate),


About Ronnie Diaz

Ronnie Diaz is a software engineer and tech consultant. Ronnie started his career in front-end and back-end development for companies in ecommerce, service industries and remote education. This work transitioned from traditional desktop client-server applications through early cloud development. Software included human resource management and service technician workflows, online retail e-commerce and electronic ordering and fulfillment, IVR customer relational systems, and video streaming remote learning SCORM web applications. Hands on server experience and software performance optimization led to creation of a startup business focused on collocated data center services and continued experience with video streaming hardware and software. This led to a career in Amazon Prime Video where Ronnie is currently employed, building software and systems which stream live sports and events for millions of viewers around the world.

Posted on May 3, 2011, in Programming & Development and tagged , , , , , , , , , , . Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: