Search code examples
c#system.diagnostics

How do I close a process once I manually shut down another?


I'm simply trying to make a console application to launch my game with a tool (The tool helps the game with some of the bugs it has). Once I'm done with the game I want to shutdown the game via the in-game menu, and then I want it to close the tool along with it without having to manually shut it down. I can't exactly seem to get it to work though, the tool just stay open after my game closes if I use "flawlessWideScreen.Close();", and if I use "flawlessWideScreen.Kill();" It throws an exception.

Anyone know what I'm doing wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;

namespace Bioshock2AdvancedLauncher
{
    class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo bioshock2Info = new ProcessStartInfo();
            bioshock2Info.FileName = (@"F:\Steam Library\steamapps\common\BioShock 2\SP\Builds\Binaries\Bioshock2.exe");
            bioshock2Info.WorkingDirectory = Path.GetDirectoryName(@"F:\Steam Library\steamapps\common\BioShock 2\SP\Builds\Binaries\Bioshock2.exe");
            Process bioshock2 = new Process();
            bioshock2.EnableRaisingEvents = true;
            bioshock2 = Process.Start(bioshock2Info);

            Process flawlessWideScreen = new Process();
            flawlessWideScreen = Process.Start(@"F:\Steam Library\steamapps\common\BioShock 2\SP\Builds\Binaries\FlawlessWidescreen.lnk");

            bioshock2.WaitForExit();

            flawlessWideScreen.Kill();

        }
    }
}

Solution

  • I fixed it by replacing flawlessWideScreen.Kill(); with this code.

            foreach (Process p in Process.GetProcesses())
            {
                string name = p.ProcessName.ToLower();
                if (name == "flawlesswidescreen") p.Kill();
            }