# Thursday, October 29, 2009
« Tip of the day: Validate string meets le... | Main | Tip of the Day: Using generics to simpli... »

Every once in a while, you need to open another application from your WinForm application. Today’s snippet is a wrapper which will give you the ability to start a new process (application). It takes two parameters: 
    filename – string - the name full path of the application to open
    waitToFinish – bool - True/False – setting to true will cause your program to stop until the opened application finishes processing.

        private static void OpenApplication(string fileName, bool waitToFinish)

        {

            OpenApplication(fileName, String.Empty, waitToFinish);

        }

 

        private static void OpenApplication(string fileName, string arguments, bool waitToFinish)

        {

            using (System.Diagnostics.Process prc = new System.Diagnostics.Process())

            {

 

                prc.StartInfo.FileName = fileName;

 

                if (arguments.Trim().Length > 0)

                    prc.StartInfo.Arguments = arguments;

 

                prc.Start();

                if (waitToFinish)

                {

                    prc.WaitForExit();

                    prc.Close();

                }

 

            }

        }

To use them, here’s an example usage:

        private void button1_Click(object sender, EventArgs e)

        {

            OpenApplication(@"c:\windows\notepad.exe", false);

            MessageBox.Show("Alert that processing has continued!", "Processing..", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

Comments are closed.