/code/src/UI/Services/RequiredVersionValidation/Validators/DotNetValidator.cs

https://github.com/Microsoft/WindowsTemplateStudio · C# · 57 lines · 46 code · 8 blank · 3 comment · 1 complexity · 776d2433d525696d029cd495ac55bc22 MD5 · raw file

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.ComponentModel;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. namespace Microsoft.Templates.UI.Services
  9. {
  10. public class DotNetValidator : IRequirementValidator
  11. {
  12. public const string Id = "dotnet";
  13. public const string DisplayName = "[.NET Core Runtime](https://dotnet.microsoft.com/download/dotnet-core/3.1)";
  14. private const string WindowsAppRuntimeName = "Microsoft.WindowsDesktop.App 3.1";
  15. public bool IsVersionInstalled(Version version)
  16. {
  17. using (var process = new Process())
  18. {
  19. process.StartInfo = new ProcessStartInfo
  20. {
  21. FileName = "dotnet",
  22. Arguments = "--list-runtimes",
  23. UseShellExecute = false,
  24. CreateNoWindow = true,
  25. RedirectStandardOutput = true,
  26. RedirectStandardError = true,
  27. };
  28. try
  29. {
  30. process.Start();
  31. var result = process.StandardOutput.ReadToEnd();
  32. process.WaitForExit();
  33. var runtimeVersions = result.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
  34. .Where(v => v.StartsWith(WindowsAppRuntimeName, StringComparison.OrdinalIgnoreCase))
  35. .Select(r => new Version(r.Split(' ')[1]));
  36. if (runtimeVersions.Any(r => r.CompareTo(version) >= 0))
  37. {
  38. return true;
  39. }
  40. }
  41. catch (Win32Exception)
  42. {
  43. return false;
  44. }
  45. }
  46. return false;
  47. }
  48. }
  49. }