It is easy to launch a new process in C#. The following code sample launches the built-in Windows Calculator application:
However, the problem with this code is if it is called multiple times in an application (like on a button click), multiple copies of the Windows Calculator will be started. In some situations that may be desired, but often only one instance of the application is desired.
The following code demonstrates how to launch a new process in C# if it is not already running or set focus to the application if it is already running:
Things to note:
- When searching for the process, you do not include the “.exe” part of the executable name.
- The call to ShowWindow will restore the Calculator window if it has been minimized.
- The call to BringWindowToTop causes the Calculator window to become the top-most window.