private static void Fetch_Method2() { const string PATH = @"SOFTWARE\Microsoft\MSBuild\ToolsVersions\"; RegistryKey registry = Registry.LocalMachine.OpenSubKey(PATH); string[] versions = registry.GetSubKeyNames(); int largest = 0; foreach (string version in versions) { float ver = 0.0F; if (!float.TryParse(version, out ver)) { continue; } if (ver > largest) { largest = (int)ver; } } if (largest == 0.0F) { throw new Exception(NOT_FOUND_EXCEPTION_TEXT); } string versionStr = largest.ToString("F1"); registry = Registry.LocalMachine.OpenSubKey(PATH + versionStr + @"\"); string[] files = Directory.GetFiles(registry.GetValue("MSBuildToolsPath").ToString(), "MSBuild.exe"); if (files.Length == 0) { throw new Exception(NOT_FOUND_EXCEPTION_TEXT); } Path = files[0]; switch (largest) { case 14: ToolsVersion = MicrosoftVCProjectGenerator.ToolsVersions.v14_0; break; case 4: ToolsVersion = MicrosoftVCProjectGenerator.ToolsVersions.v14_1; break; default: throw new Exception(NOT_FOUND_EXCEPTION_TEXT); } }
private static void Fetch_Method1() { //Visual Studio 6 - 6.0 //Visual Studio .NET(2002) - 7.0 //Visual Studio .NET 2003 - 7.1 //Visual Studio 2005 - 8.0 //Visual Studio 2008 - 9.0 //Visual Studio 2010 - 10.0 //Visual Studio 2012 - 11.0 //Visual Studio 2013 - 12.0 //Visual Studio 2015 - 14.0 //Visual Studio 2017 - 15.0 //Visual Studio 2019 - 16.0 const string VS_WHERE_PATH = "C:/Program Files (x86)/Microsoft Visual Studio/Installer/vswhere.exe"; if (!File.Exists(VS_WHERE_PATH)) { throw new Exception(NOT_FOUND_EXCEPTION_TEXT); } CommandLineProcess process = new CommandLineProcess(); process.FilePath = VS_WHERE_PATH; process.Start(); string output = process.Output.ReadToEnd(); const string PATH_KEY = "installationPath: "; int index = output.IndexOf(PATH_KEY); index += PATH_KEY.Length; int endOfLineIndex = output.IndexOf('\r', index); string path = output.Substring(index, endOfLineIndex - index); string[] files = Directory.GetFiles(path, "MSBuild.exe", SearchOption.AllDirectories); if (files.Length == 0) { throw new Exception(NOT_FOUND_EXCEPTION_TEXT); } Path = files[0]; const string VERSION_KEY = "installationVersion: "; index = output.IndexOf(VERSION_KEY); index += VERSION_KEY.Length; int firstPointIndex = output.IndexOf('.', index); int version = Convert.ToInt32(output.Substring(index, firstPointIndex - index)); switch (version) { case 14: ToolsVersion = MicrosoftVCProjectGenerator.ToolsVersions.v14_0; break; case 15: ToolsVersion = MicrosoftVCProjectGenerator.ToolsVersions.v14_1; break; case 16: ToolsVersion = MicrosoftVCProjectGenerator.ToolsVersions.v14_2; break; default: throw new Exception(NOT_FOUND_EXCEPTION_TEXT); } }