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 an enterprise software engineer responsible for front-end and back-end development for companies in many industries. Heavily involved in cloud development, online retail, e-commerce and electronic ordering, fulfillment and customer relational systems.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: