private static bool CheckInstance(ISetupInstance2 setupInstance2, ref StringBuilder log) { // Visual Studio Community 2017 component directory: // https://www.visualstudio.com/en-us/productinfo/vs2017-install-product-Community.workloads string path = setupInstance2.GetInstallationPath().Replace("\\", "\\\\"); log.Append(String.Format("Found installation at: {0}\\n", path)); bool hasMSBuild = false; bool hasVCTools = false; uint Win10SDKVer = 0; bool hasWin8SDK = false; foreach (ISetupPackageReference package in setupInstance2.GetPackages()) { const string Win10SDKPrefix = "Microsoft.VisualStudio.Component.Windows10SDK."; string id = package.GetId(); if (id == "Microsoft.VisualStudio.VC.MSBuild.Base") { hasMSBuild = true; } else if (id == "Microsoft.VisualStudio.Component.VC.Tools.x86.x64") { hasVCTools = true; } else if (id.StartsWith(Win10SDKPrefix)) { string[] parts = id.Substring(Win10SDKPrefix.Length).Split('.'); if (parts.Length > 1 && parts[1] != "Desktop") { continue; } Win10SDKVer = Math.Max(Win10SDKVer, UInt32.Parse(parts[0])); } else if (id == "Microsoft.VisualStudio.Component.Windows81SDK") { hasWin8SDK = true; } else { continue; } log.Append(String.Format(" - Found {0}\\n", id)); } if (!hasMSBuild) { log.Append(" - Missing Visual Studio C++ core features (Microsoft.VisualStudio.VC.MSBuild.Base)\\n"); } if (!hasVCTools) { log.Append(" - Missing VC++ 2017 v141 toolset (x86,x64) (Microsoft.VisualStudio.Component.VC.Tools.x86.x64)\\n"); } if ((Win10SDKVer == 0) && (!hasWin8SDK)) { log.Append(" - Missing a Windows SDK (Microsoft.VisualStudio.Component.Windows10SDK.* or Microsoft.VisualStudio.Component.Windows81SDK)\\n"); } if (hasMSBuild && hasVCTools) { if (Win10SDKVer > 0) { log.Append(" - Using this installation with Windows 10 SDK" /*\\n*/); Console.WriteLine(String.Format("{{\"log\":\"{0}\",\"path\":\"{1}\",\"sdk\":\"10.0.{2}.0\"}}", log.ToString(), path, Win10SDKVer)); return(true); } else if (hasWin8SDK) { log.Append(" - Using this installation with Windows 8.1 SDK" /*\\n*/); Console.WriteLine(String.Format("{{\"log\":\"{0}\",\"path\":\"{1}\",\"sdk\":\"8.1\"}}", log.ToString(), path)); return(true); } } log.Append(" - Some required components are missing, not using this installation\\n"); return(false); }
private string GetMSBuildExePathForVersion(string version) => Path.Combine(_visualStudioInstance.GetInstallationPath(), "MSBuild", version, "Bin", "MSBuild.exe");
/// <summary> /// Gets the installed Visual Studio instances (the first item is the latest version). /// </summary> /// <returns>The install locations.</returns> public static IReadOnlyList <VisualStudioInstance> GetInstances() { if (_installDirs == null) { _installDirs = new List <VisualStudioInstance>(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { // Visual Studio 2017-2020 List <VisualStudioInstance> preReleaseInstallDirs = null; try { SetupConfiguration setup = new SetupConfiguration(); IEnumSetupInstances enumerator = setup.EnumAllInstances(); ISetupInstance[] instances = new ISetupInstance[1]; while (true) { enumerator.Next(1, instances, out int fetchedCount); if (fetchedCount == 0) { break; } ISetupInstance2 instance = (ISetupInstance2)instances[0]; if ((instance.GetState() & InstanceState.Local) == InstanceState.Local) { VisualStudioVersion version; string displayName = instance.GetDisplayName(); if (displayName.Contains("2019")) { version = VisualStudioVersion.VisualStudio2019; } else if (displayName.Contains("2017")) { version = VisualStudioVersion.VisualStudio2017; } else { throw new Exception(string.Format("Unknown Visual Studio installation. Display name: {0}", displayName)); } var vsInstance = new VisualStudioInstance { Version = version, Path = instance.GetInstallationPath(), }; if (instance is ISetupInstanceCatalog catalog && catalog.IsPrerelease()) { if (preReleaseInstallDirs == null) { preReleaseInstallDirs = new List <VisualStudioInstance>(); } preReleaseInstallDirs.Add(vsInstance); } else { _installDirs.Add(vsInstance); } } } }
private static VSInstance ParseInstance(ISetupInstance2 setupInstance2) { VSInstance inst = new VSInstance(); string[] prodParts = setupInstance2.GetProduct().GetId().Split('.'); Array.Reverse(prodParts); inst["Product"] = prodParts[0]; inst["ID"] = setupInstance2.GetInstanceId(); inst["Name"] = setupInstance2.GetDisplayName(0x1000); inst["Description"] = setupInstance2.GetDescription(0x1000); inst["InstallationName"] = setupInstance2.GetInstallationName(); inst["Version"] = setupInstance2.GetInstallationVersion(); inst["State"] = Enum.GetName(typeof(InstanceState), setupInstance2.GetState()); inst["InstallationPath"] = setupInstance2.GetInstallationPath(); inst["IsComplete"] = setupInstance2.IsComplete(); inst["IsLaunchable"] = setupInstance2.IsLaunchable(); inst["Common7ToolsPath"] = inst["InstallationPath"] + @"\Common7\Tools\"; inst["CmdPath"] = inst["Common7ToolsPath"] + "VsDevCmd.bat"; inst["VCAuxiliaryBuildPath"] = inst["InstallationPath"] + @"\VC\Auxiliary\Build\"; inst["VCVarsAllPath"] = inst["VCAuxiliaryBuildPath"] + "vcvarsall.bat"; inst["Win8SDK"] = ""; inst["SDK10Full"] = ""; inst["VisualCppTools"] = ""; Regex trimmer = new Regex(@"\.\d+$"); List <string> packs = new List <String>(); foreach (ISetupPackageReference package in setupInstance2.GetPackages()) { string id = package.GetId(); string ver = package.GetVersion(); string detail = "{\"id\": \"" + id + "\", \"version\":\"" + ver + "\"}"; packs.Add(" " + detail); if (id.Contains("Component.MSBuild")) { inst["MSBuild"] = detail; inst["MSBuildVerFull"] = ver; } else if (id.Contains("Microsoft.VisualCpp.Tools.Core")) { inst["VCTools"] = detail; inst["VisualCppToolsFullVersion"] = ver; string majorMinor = trimmer.Replace(trimmer.Replace(ver, ""), ""); inst["VisualCppToolsVersionMinor"] = majorMinor; inst["VCToolsVersionCode"] = "vc" + majorMinor.Replace(".", ""); } else if (id.Contains("Microsoft.Windows.81SDK")) { if (inst["Win8SDK"].ToString().CompareTo(ver) > 0) { continue; } inst["Win8SDK"] = ver; } else if (id.Contains("Win10SDK_10")) { if (inst["SDK10Full"].ToString().CompareTo(ver) > 0) { continue; } inst["SDK10Full"] = ver; } } packs.Sort(); inst["Packages"] = packs.ToArray(); string[] sdk10Parts = inst["SDK10Full"].ToString().Split('.'); sdk10Parts[sdk10Parts.Length - 1] = "0"; inst["SDK10"] = String.Join(".", sdk10Parts); inst["SDK"] = inst["SDK10"].ToString() != "0" ? inst["SDK10"] : inst["Win8SDK"]; if (inst.ContainsKey("MSBuildVerFull")) { string ver = trimmer.Replace(trimmer.Replace((string)inst["MSBuildVerFull"], ""), ""); inst["MSBuildVer"] = ver; inst["MSBuildToolsPath"] = inst["InstallationPath"] + @"\MSBuild\" + ver + @"\Bin\"; inst["MSBuildPath"] = inst["MSBuildToolsPath"] + "MSBuild.exe"; } if (inst.ContainsKey("VCTools")) { string ver = trimmer.Replace((string)inst["VisualCppToolsFullVersion"], ""); inst["VisualCppToolsX64"] = inst["InstallationPath"] + @"\VC\Tools\MSVC\" + ver + @"\bin\HostX64\x64\"; inst["VisualCppToolsX64CL"] = inst["VisualCppToolsX64"] + "cl.exe"; inst["VisualCppToolsX86"] = inst["InstallationPath"] + @"\VC\Tools\MSVC\" + ver + @"\bin\HostX86\x86\"; inst["VisualCppToolsX86CL"] = inst["VisualCppToolsX86"] + "cl.exe"; inst["VisualCppTools"] = Is64() ? inst["VisualCppToolsX64"] : inst["VisualCppToolsX86"]; inst["VisualCppToolsCL"] = Is64() ? inst["VisualCppToolsX64CL"] : inst["VisualCppToolsX86CL"]; } inst["IsVcCompatible"] = inst.JSONBool("SDK") && inst.JSONBool("MSBuild") && inst.JSONBool("VisualCppTools"); return(inst); }
static List <VisualStudioInstallation> GetVisualStudioInstallations() { CachedInstalls.Clear(); try { SetupConfiguration Setup = new SetupConfiguration(); IEnumSetupInstances Enumerator = Setup.EnumAllInstances(); ISetupInstance[] Instances = new ISetupInstance[1]; for (; ;) { int NumFetched; Enumerator.Next(1, Instances, out NumFetched); if (NumFetched == 0) { break; } ISetupInstance2 Instance = (ISetupInstance2)Instances[0]; if ((Instance.GetState() & InstanceState.Local) == InstanceState.Local) { string VersionString = Instance.GetInstallationVersion(); string[] Components = VersionString.Split('.'); if (Components.Length == 0) { continue; } int MajorVersion; string InstallationPath = Instance.GetInstallationPath(); string DevEnvPath = Path.Combine(InstallationPath, "Common7\\IDE\\devenv.exe"); if (!int.TryParse(Components[0], out MajorVersion) || (MajorVersion != 15 && MajorVersion != 16)) { continue; } if (!File.Exists(DevEnvPath)) { continue; } VisualStudioInstallation Installation = new VisualStudioInstallation() { BaseDir = InstallationPath, DevEnvPath = DevEnvPath, MajorVersion = MajorVersion, ROTMoniker = string.Format("!VisualStudio.DTE.{0}.0", MajorVersion) }; CachedInstalls.Add(Installation); } } } catch (Exception Ex) { MessageBox.Show(string.Format("Exception while finding Visual Studio installations {0}", Ex.Message)); } // prefer newer versions CachedInstalls.Sort((A, B) => { return(-A.MajorVersion.CompareTo(B.MajorVersion)); }); return(CachedInstalls); }