Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS has been phased out. To see alternatives please check here

Skip to content
Snippets Groups Projects
Select Git revision
  • f576921a84fa6485adf2954f4b2c28461acadc54
  • amdm2/main default
  • doc-fixes
  • amdm3/develop
  • vehmetrics
  • feature/generic_vehicles_tests
  • trailer-stable
  • trailer
  • pubcode
  • bugfix/CodeEU-547_MaxICEOffTimespan
  • ci
  • stable protected
  • bugfix/CodeEU-235_Tyre_XML-type_VIF
  • revert-f849ae0c
  • Project_VECTO_FD_II/FMI/AMTShiftStratgy
  • Project_VECTO_FD_II/FMI/HybridController
  • feature/CodeEU-51_Extend-ADC-lossmap-declaration-mode
  • tug-dev
  • develop
  • hm_feature/vecto-40-add-nuget
  • tug-stable
  • Release/v4.1.0.3392-RC
  • Release/v4.0.3.3330
  • Release/v4.0.2.3275
  • Release/v4.0.2.3273
  • Release/v0.11.1.3228-DEV
  • Project_VECTO_FD_II/FMI/HybridController
  • Project_VECTO_FD_II/FMI/AMTShiftStrategy
  • Release/v4.0.1.3217
  • Release/v4.0.0.3211
  • Release/v0.11.0.3193-DEV
  • Release/v4.0.0.3161-RC
  • Release/v4.0.0.3106-RC
  • Release/v3.3.15.3102
  • Release/v4.0.0.3078-RC
  • Release/v3.3.15.3073-RC
  • test-tag
  • Release/v3.3.14.3011
  • Build/v0.7.10.2996
  • v0.7.5b0+2524.multistep
  • Release/v3.3.14.2981-RC
41 results

StarterHelper.cs

Blame
  • Forked from VECTO / VECTO Sim
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    StarterHelper.cs 2.42 KiB
    using Microsoft.Win32;
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.IO;
    using System.Reflection;
    
    namespace TUGraz.VECTO
    {
    	class StarterHelper
    	{
    		public static void StartVECTO(string[] args, params string[] validVersions)
    		{
    			var path = "No path found.";
    			string version = "No version found.";
    			if (validVersions is null) {
    				validVersions = new[] { "net45", "net48", "net60" };
    			}
    			try {
    				if (args.Length > 0) {
    					version = args[0].ToLower();
    				} else {
    					version = StarterHelper.GetHighestNETVersion();
    				}
    
    				path = $"{version}\\{Assembly.GetExecutingAssembly().GetName().Name}.exe";
    				Process.Start(new ProcessStartInfo(path) {
    					WorkingDirectory = Directory.GetCurrentDirectory()
    				});
    				ValidateVersion(version, validVersions);
    			} catch (Exception e) {
    				var message = $"Error during starting VECTO.\nDetected .NET version: {version}\nTried to open path: {path}\n{e.Message}";
    				File.AppendAllText("LOG.txt", $"{DateTime.Now} {message}\n");
    				Console.WriteLine(message);
    				throw new Exception(message);
    			}
    		}
    		
    		private static void ValidateVersion(string version, params string[] validVersions)
    		{
    			if (!((IList)validVersions).Contains(version))
    				throw new Exception($"Invalid .NET Version supplied. Only the following values are valid: {string.Join(", ", validVersions)}");
    		}
    		
    		private static string GetHighestNETVersion()
    		{
    			if (SupportsNet60()) {
    				return "net60";
    			}
    
    			if (SupportsNet48()) {
    				return "net48";
    			}
    
    			return "net45";
    		}
    
    		private static bool SupportsNet60()
    		{
    			try {
    				var p = Process.Start(new ProcessStartInfo("dotnet", "--list-runtimes") {
    					CreateNoWindow = true,
    					UseShellExecute = false,
    					RedirectStandardError = true,
    					RedirectStandardOutput = true
    				});
    
    				p.WaitForExit();
    				var output = p.StandardOutput.ReadToEnd();
    				return output.Contains("Microsoft.WindowsDesktop.App 6");
    			} catch (Exception e) {
    				Console.WriteLine(e);
    			}
    
    			return false;
    		}
    
    		private static bool SupportsNet48()
    		{
    			const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
    			using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey)) {
    				if (ndpKey != null && ndpKey.GetValue("Release") != null) {
    					var releaseKey = (int)ndpKey.GetValue("Release");
    					return releaseKey >= 528040;
    				}
    
    				return false;
    			}
    		}
    
    	}
    }