using System; using System.Collections.Concurrent; using System.Diagnostics; namespace RoslynWS { /// /// Information about the .NET Core runtime. /// public class DotNetRuntimeInfo { /// /// A cache of .NET runtime information by target directory. /// static readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); /// /// The .NET Core version. /// public string Version { get; set; } /// /// The .NET Core base directory. /// public string BaseDirectory { get; set; } /// /// The current runtime identifier (RID). /// public string RID { get; set; } /// /// Get information about the current .NET Core runtime. /// /// /// An optional base directory where dotnet.exe should be run (this may affect the version it reports due to global.json). /// /// /// A containing the runtime information. /// public static DotNetRuntimeInfo GetCurrent(string baseDirectory = null) { return _cache.GetOrAdd(baseDirectory, _ => { DotNetRuntimeInfo runtimeInfo = new DotNetRuntimeInfo(); Process dotnetInfoProcess = Process.Start(new ProcessStartInfo { FileName = "dotnet", WorkingDirectory = baseDirectory, Arguments = "--info", UseShellExecute = false, RedirectStandardOutput = true }); using (dotnetInfoProcess) { dotnetInfoProcess.WaitForExit(); string currentSection = null; string currentLine; while ((currentLine = dotnetInfoProcess.StandardOutput.ReadLine()) != null) { if (String.IsNullOrWhiteSpace(currentLine)) continue; if (!currentLine.StartsWith(" ")) { currentSection = currentLine; continue; } string[] property = currentLine.Split(new char[] { ':' }, count: 2); if (property.Length != 2) continue; property[0] = property[0].Trim(); property[1] = property[1].Trim(); switch (currentSection) { case "Product Information:": { switch (property[0]) { case "Version": { runtimeInfo.Version = property[1]; break; } } break; } case "Runtime Environment:": { switch (property[0]) { case "Base Path": { runtimeInfo.BaseDirectory = property[1]; break; } case "RID": { runtimeInfo.RID = property[1]; break; } } break; } } } } return runtimeInfo; }); } /// /// Clear the cache of .NET runtime information. /// public static void ClearCache() { _cache.Clear(); } } }