public static bool FindVSVersion() { 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.GetDisplayName(); if (VersionString.Contains("19")) { return(true); } } } } catch { } return(true); }
private static VisualStudioInstance FillInInstanceData(ISetupInstance2 instance, int lcid, Boolean includePackages) { VisualStudioInstance result = new VisualStudioInstance() { InstanceId = instance.GetInstanceId(), InstalledVersionNumber = instance.GetInstallationVersion(), Description = instance.GetDescription(lcid), DisplayName = instance.GetDisplayName(lcid) }; // Hides the non-CLS clompliant uint. var tempState = instance.GetState(); if (tempState == InstanceState.Complete) { result.Status = InstanceStatus.Complete; } else { result.Status = (InstanceStatus)tempState; } result.InstallationPath = instance.GetInstallationPath(); ISetupPackageReference prod = instance.GetProduct(); if (prod != null) { result.ProductName = prod.GetId(); } if ((result.Status & InstanceStatus.Local) == InstanceStatus.Local) { result.InstallationPath = instance.GetInstallationPath(); } if (includePackages) { ProcessPackages(instance, result); } return(result); }
public VisualStudioInstance(ISetupInstance2 FromInstance) { this.Packages = new List <VisualStudioPackage>(); this.Name = FromInstance.GetInstallationName(); this.Path = FromInstance.GetInstallationPath(); this.InstallationVersion = FromInstance.GetInstallationVersion(); this.DisplayName = FromInstance.GetDisplayName(); this.ResolvePath = FromInstance.ResolvePath(); this.InstanceId = FromInstance.GetInstanceId(); this.ProductPath = FromInstance.GetProductPath(); this.State = FromInstance.GetState(); var packages = FromInstance.GetPackages(); foreach (var item in packages) { this.Packages.Add(new VisualStudioPackage(item)); } }
private string GetVS2019Path() { try { SetupConfiguration setupConfiguration = new SetupConfiguration(); ISetupInstance[] iSetupInstance = new ISetupInstance[1]; IEnumSetupInstances iEnumSetupInstances = setupConfiguration.EnumAllInstances(); while (true) { { int test; iEnumSetupInstances.Next(1, iSetupInstance, out test); if (test == 0) { break; } } ISetupInstance2 iSetupInstance2 = (ISetupInstance2)iSetupInstance[0]; if ((iSetupInstance2.GetState() & InstanceState.Local) == InstanceState.Local) { string InstallationVersion = iSetupInstance2.GetInstallationVersion(); if (!string.IsNullOrEmpty(InstallationVersion)) { string vs = InstallationVersion.Remove(InstallationVersion.IndexOf('.')); if (vs == "16") { return(iSetupInstance2.GetInstallationPath()); } } } } } catch { } throw new Exception("Visual Studio 2019 неустановлена."); return(null); }
private static string GetPathOfFirstInstalledVisualStudioInstance() { Tuple <Version, string> highestVersion = null; try { IEnumSetupInstances setupInstances = new SetupConfiguration().EnumAllInstances(); ISetupInstance[] instances = new ISetupInstance[1]; for (setupInstances.Next(1, instances, out int fetched); fetched > 0; setupInstances.Next(1, instances, out fetched)) { ISetupInstance2 instance = (ISetupInstance2)instances.First(); if (instance.GetState() == InstanceState.Complete) { string installationPath = instance.GetInstallationPath(); if (!string.IsNullOrWhiteSpace(installationPath) && Version.TryParse(instance.GetInstallationVersion(), out Version version)) { if (highestVersion == null || version > highestVersion.Item1) { highestVersion = new Tuple <Version, string>(version, installationPath); } } } } if (highestVersion != null) { return(Path.Combine(highestVersion.Item2, "MSBuild", GetMSBuildVersionDirectory($"{highestVersion.Item1.Major}.0"), "Bin")); } } catch { // Ignored } return(null); }
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); }
/// <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); } } } }
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); }