Riot IngameAPI - Random Port Fix (Quick & Dirty)

09/04/2020 12:56 Interløgy#1
Hey,

As you have probably noticed, the ports for the League Ingame Api are random (before always 2999).
This was probably not intended by Riot. When a patch will be released is still written in the stars. :feelsbadman:


The code is not written properly, but it works.
It is not a beauty contest. It's just a workaround until the next patch.
To get the port for the ingame API you can use the following code:

Code:
private String GetPort()
        {
            var processes = Process.GetProcessesByName("League of Legends");

            using (var ns = new Process())
            {
                ProcessStartInfo psi = new ProcessStartInfo("netstat.exe", "-ano");
                psi.RedirectStandardOutput = true;
                psi.UseShellExecute = false;
                ns.StartInfo = psi;
                ns.Start();

                using (StreamReader r = ns.StandardOutput)
                {
                    string output = r.ReadToEnd();
                    ns.WaitForExit();

                    string[] lines = output.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

                    foreach (string line in lines)
                    {
                        if (line.Contains(processes[0].Id.ToString()) && line.Contains("0.0.0.0:0"))
                        {
                            var outp = line.Split(' ');
                            return outp[6].Replace("127.0.0.1:", "");
                        }
                    }
                }
            }
            return String.Empty;
        }
09/04/2020 12:58 LeagueAddons#2
Great job!
Thank you for this <3