diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 415fab3e..05f3dc8d 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -82,22 +82,34 @@ private static IEnumerable GetDotNetBasePaths(string workingDirectory) const string DOTNET_CLI_UI_LANGUAGE = nameof(DOTNET_CLI_UI_LANGUAGE); Process process; + var lines = new List(); try { - var startInfo = new ProcessStartInfo("dotnet", "--info") - { - WorkingDirectory = workingDirectory, - CreateNoWindow = true, - UseShellExecute = false, - RedirectStandardOutput = true, - RedirectStandardError = true + process = new Process() + { + StartInfo = new ProcessStartInfo("dotnet", "--info") + { + WorkingDirectory = workingDirectory, + CreateNoWindow = true, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true + } }; // Ensure that we set the DOTNET_CLI_UI_LANGUAGE environment variable to "en-US" before // running 'dotnet --info'. Otherwise, we may get localized results. - startInfo.EnvironmentVariables[DOTNET_CLI_UI_LANGUAGE] = "en-US"; + process.StartInfo.EnvironmentVariables[DOTNET_CLI_UI_LANGUAGE] = "en-US"; + + process.OutputDataReceived += (_, e) => + { + if (!string.IsNullOrWhiteSpace(e.Data)) + { + lines.Add(e.Data); + } + }; - process = Process.Start(startInfo); + process.Start(); } catch { @@ -105,20 +117,6 @@ private static IEnumerable GetDotNetBasePaths(string workingDirectory) yield break; } - if (process.HasExited) - { - yield break; - } - - var lines = new List(); - process.OutputDataReceived += (_, e) => - { - if (!string.IsNullOrWhiteSpace(e.Data)) - { - lines.Add(e.Data); - } - }; - process.BeginOutputReadLine(); process.WaitForExit(); @@ -126,14 +124,12 @@ private static IEnumerable GetDotNetBasePaths(string workingDirectory) var outputString = string.Join(Environment.NewLine, lines); var matched = DotNetBasePathRegex.Match(outputString); - if (!matched.Success) + string basePath = null; + if (matched.Success) { - yield break; + basePath = matched.Groups[1].Value.Trim(); + yield return basePath; } - - var basePath = matched.Groups[1].Value.Trim(); - - yield return basePath; // We return the version in use at the front of the list in order to ensure FirstOrDefault always returns the version in use. var lineSdkIndex = lines.FindIndex(line => line.Contains("SDKs installed"));