public static bool TryResolveDotNetCoreSdk()

in src/Shared/DotNetCoreSdkResolver.cs [29:121]


        public static bool TryResolveDotNetCoreSdk(FileInfo dotnetFileInfo, out DirectoryInfo basePath)
        {
            basePath = null;

            string parsedBasePath = null;

            using ManualResetEvent processExited = new ManualResetEvent(false);
            using Process process = new Process
            {
                EnableRaisingEvents = true,
                StartInfo = new ProcessStartInfo
                {
                    Arguments = "--info",
                    CreateNoWindow = true,
                    FileName = "dotnet",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    WorkingDirectory = Environment.CurrentDirectory,
                },
            };

            process.StartInfo.EnvironmentVariables["DOTNET_CLI_UI_LANGUAGE"] = "en-US";
            process.StartInfo.EnvironmentVariables["DOTNET_CLI_TELEMETRY_OPTOUT"] = bool.TrueString;

            process.OutputDataReceived += (_, args) =>
            {
                if (!string.IsNullOrWhiteSpace(args.Data))
                {
                    Match match = DotNetBasePathRegex.Match(args.Data);

                    if (match.Success && match.Groups["Path"].Success)
                    {
                        parsedBasePath = match.Groups["Path"].Value.Trim();
                    }
                }
            };

            process.Exited += (_, _) =>
            {
                processExited.Set();
            };

            try
            {
                if (!process.Start())
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }

            process.BeginOutputReadLine();

            switch (WaitHandle.WaitAny(new WaitHandle[] { processExited }, TimeSpan.FromSeconds(5)))
            {
                case WaitHandle.WaitTimeout:
                    break;

                case 0:
                    break;
            }

            if (!process.HasExited)
            {
                try
                {
                    process.Kill();
                }
                catch (Exception)
                {
                    // Ignored
                }
            }

            if (!string.IsNullOrWhiteSpace(parsedBasePath))
            {
                basePath = new DirectoryInfo(parsedBasePath);

                return true;
            }

            Console.ForegroundColor = ConsoleColor.Red;
            Console.BackgroundColor = ConsoleColor.Black;

            ResolveSdk(dotnetFileInfo.FullName);

            Console.ResetColor();

            return false;
        }