c# unable to kill process

Viewed 754

Basically I am trying to make a program that will be able to launch and close a .bat file. I have managed to get it to launch the bat file, however I am unable to figure out how to get it to close the .bat file.

Here is the class code:

using System;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;

namespace Launcher
{
    class MyProcess
    {
        public static void LaunchProcess()
        {
            Process myProcess = new Process();
            try
            {
                myProcess.StartInfo.UseShellExecute = false;
                myProcess.StartInfo.FileName = "C:\example.bat";
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

I am aware that to kill the process I would have to use myProcess.Kill(); however I am unsure how to add it to the code, as if I was to re-call the 'launchprocess' then the myProcess would be overwritten

2 Answers
Related