public VisualStudioInstance(ISetupInstance2 FromInstance) { this.IsLaunchable = FromInstance.IsLaunchable(); this.IsComplete = FromInstance.IsComplete(); this.Name = FromInstance.GetInstallationName(); this.Path = FromInstance.GetInstallationPath(); this.Version = FromInstance.GetInstallationVersion(); this.DisplayName = FromInstance.GetDisplayName(); this.Description = FromInstance.GetDescription(); this.ResolvePath = FromInstance.ResolvePath(); this.EnginePath = FromInstance.GetEnginePath(); this.InstanceId = FromInstance.GetInstanceId(); this.ProductPath = FromInstance.GetProductPath(); try { var time = FromInstance.GetInstallDate(); ulong high = (ulong)time.dwHighDateTime; uint low = (uint)time.dwLowDateTime; long fileTime = (long)((high << 32) + low); this.InstallDate = DateTime.FromFileTimeUtc(fileTime); } catch { this.InstallDate = DateTime.UtcNow; } // FromInstance.GetState(); // FromInstance.GetPackages(); // FromInstance.GetProduct(); // FromInstance.GetProperties(); // FromInstance.GetErrors(); }
public static VisualStudioInstance GetVisualStudioInstance() { if (VisualStudioInstanceList == null) { VisualStudioInstanceList = new List <VisualStudioInstance>(); try { var setupConfiguration = new SetupConfiguration() as ISetupConfiguration2; var instanceEnumerator = setupConfiguration.EnumAllInstances(); var instances = new ISetupInstance2[3]; instanceEnumerator.Next(instances.Length, instances, out var instancesFetched); if (instancesFetched == 0) { return(null); } do { for (int i = 0; i < instancesFetched; i++) { VisualStudioInstanceList.Add(new VisualStudioInstance(instances[i])); } instanceEnumerator.Next(instances.Length, instances, out instancesFetched); }while (instancesFetched != 0); } catch { } } return(VisualStudioInstanceList[0]); }
public static List <VSInstance> QueryEx(params string[] args) { List <VSInstance> insts = new List <VSInstance>(); ISetupConfiguration query = new SetupConfiguration(); ISetupConfiguration2 query2 = (ISetupConfiguration2)query; IEnumSetupInstances e = query2.EnumAllInstances(); ISetupInstance2[] rgelt = new ISetupInstance2[1]; int pceltFetched; e.Next(1, rgelt, out pceltFetched); while (pceltFetched > 0) { ISetupInstance2 raw = rgelt[0]; insts.Add(ParseInstance(raw)); e.Next(1, rgelt, out pceltFetched); } foreach (VSInstance inst in insts.ToArray()) { foreach (string key in args) { if (!inst.JSONBool(key)) { insts.Remove(inst); } } if (Array.IndexOf(args, "Packages") == -1) { inst.Remove("Packages"); } } return(insts); }
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 string InstanceJson(ISetupInstance2 setupInstance2) { // Visual Studio component directory: // https://docs.microsoft.com/en-us/visualstudio/install/workload-and-component-ids StringBuilder json = new StringBuilder(); json.Append("{"); string path = JsonString(setupInstance2.GetInstallationPath()); json.Append(String.Format("\"path\":{0},", path)); string version = JsonString(setupInstance2.GetInstallationVersion()); json.Append(String.Format("\"version\":{0},", version)); List <string> packages = new List <string>(); foreach (ISetupPackageReference package in setupInstance2.GetPackages()) { string id = JsonString(package.GetId()); packages.Add(id); } json.Append(String.Format("\"packages\":[{0}]", string.Join(",", packages.ToArray()))); json.Append("}"); return(json.ToString()); }
public static void PrintJson() { ISetupConfiguration query = new SetupConfiguration(); ISetupConfiguration2 query2 = (ISetupConfiguration2)query; IEnumSetupInstances e = query2.EnumAllInstances(); int pceltFetched; ISetupInstance2[] rgelt = new ISetupInstance2[1]; List <string> instances = new List <string>(); while (true) { e.Next(1, rgelt, out pceltFetched); if (pceltFetched <= 0) { Console.WriteLine(String.Format("[{0}]", string.Join(",", instances.ToArray()))); return; } try { instances.Add(InstanceJson(rgelt[0])); } catch (COMException) { // Ignore instances that can't be queried. } } }
public static void Query() { ISetupConfiguration query = new SetupConfiguration(); ISetupConfiguration2 query2 = (ISetupConfiguration2)query; IEnumSetupInstances e = query2.EnumAllInstances(); int pceltFetched; ISetupInstance2[] rgelt = new ISetupInstance2[1]; StringBuilder log = new StringBuilder(); while (true) { e.Next(1, rgelt, out pceltFetched); if (pceltFetched <= 0) { Console.WriteLine(String.Format("{{\"log\":\"{0}\"}}", log.ToString())); return; } if (CheckInstance(rgelt[0], ref log)) { return; } } }
private void WriteInstance(ISetupInstance2 instance) { if (instance != null) { var adapted = new Instance(instance); WriteObject(adapted); } }
private static void ProcessPackages(ISetupInstance2 instance, VisualStudioInstance result) { ISetupPackageReference[] packages = instance.GetPackages(); foreach (var package in packages) { String packageType = package.GetType(); PackageReference refPackage = CreatePackageReference(package); switch (packageType.ToUpper(CultureInfo.InvariantCulture)) { case "PRODUCT": result.Products.Add(refPackage); break; case "WORKLOAD": result.Workloads.Add(refPackage); break; case "COMPONENT": result.Components.Add(refPackage); break; case "VSIX": result.Visx.Add(refPackage); break; case "EXE": result.Exe.Add(refPackage); break; case "MSI": result.Msi.Add(refPackage); break; case "MSU": result.Msu.Add(refPackage); break; case "GROUP": result.Group.Add(refPackage); break; case "WINDOWSFEATURE": result.WindowsFeature.Add(refPackage); break; default: Trace.WriteLine(packageType); result.OtherPackages.Add(refPackage); break; } } }
public static IEnumerable <VsInstallation> FindInstallations() { using (var visualStudioRegKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio")) { if (visualStudioRegKey != null) { foreach (string versionKey in visualStudioRegKey.GetSubKeyNames()) { if (!Version.TryParse(versionKey, out var version)) { continue; } using (var vs = visualStudioRegKey.OpenSubKey($@"{versionKey}\Setup\VS")) using (var devenv = Registry.LocalMachine.OpenSubKey($@"SOFTWARE\Microsoft\DevDiv\VS\Servicing\{version.Major}.0\devenv")) { var installationPath = vs?.GetValue("ProductDir") as string; var devEnvVersion = devenv?.GetValue("Version") as string; if (IsValidInstallationDirectory(installationPath)) { yield return(new VsInstallation( version: version, path: installationPath, name: $"Visual Studio/{devEnvVersion ?? version.ToString(2)}")); } } } } } var setupConfiguration = new SetupConfiguration(); var setupInstanceEnumerator = setupConfiguration.EnumInstances(); var setupInstances = new ISetupInstance2[1]; while (true) { setupInstanceEnumerator.Next(setupInstances.Length, setupInstances, out var fetched); if (fetched == 0) { break; } var instance = setupInstances[0]; if (IsValidInstallationDirectory(instance.GetInstallationPath())) { yield return(new VsInstallation( version: new Version(instance.GetInstallationVersion()), name: instance.GetInstallationName(), path: instance.GetInstallationPath()) { PackageIds = instance.GetPackages().Select(p => p.GetId()), }); } } }
private void WriteInstance(ISetupInstance2 instance, bool all = false) { if (instance != null) { var adapted = new Instance(instance); if (all || Prerelease || adapted.IsPrerelease != true) { WriteObject(adapted); } } }
private string GetExeToolPathFromSetupConfiguration(ILogger logger) { string toolPath = null; logger.LogDebug(Resources.CONV_DIAG_LocatingCodeCoverageToolSetupConfiguration); ISetupConfiguration configurationQuery = setupConfigurationFactory.GetSetupConfigurationQuery(); if (configurationQuery != null) { IEnumSetupInstances instanceEnumerator = configurationQuery.EnumInstances(); int fetched; ISetupInstance[] tempInstance = new ISetupInstance[1]; List <ISetupInstance2> instances = new List <ISetupInstance2>(); //Enumerate the configuration instances do { instanceEnumerator.Next(1, tempInstance, out fetched); if (fetched > 0) { ISetupInstance2 instance = (ISetupInstance2)tempInstance[0]; if (instance.GetPackages().Any(p => p.GetId() == CodeCoverageInstallationPackage)) { //Store instances that have code coverage package installed instances.Add((ISetupInstance2)tempInstance[0]); } } } while (fetched > 0); if (instances.Count > 1) { logger.LogDebug(Resources.CONV_DIAG_MultipleVsVersionsInstalled, string.Join(", ", instances.Select(i => i.GetInstallationVersion()))); } //Get the installation path for the latest visual studio found var visualStudioPath = instances.OrderByDescending(i => i.GetInstallationVersion()) .Select(i => i.GetInstallationPath()) .FirstOrDefault(); if (visualStudioPath != null) { toolPath = Path.Combine(visualStudioPath, TeamToolPathandExeName); } } else { logger.LogDebug(Resources.CONV_DIAG_SetupConfigurationNotSupported); } return(toolPath); }
public VCComponent(ISetupPackageReference package, ISetupInstance2 setupInstance) { this.InstallationPath = setupInstance.GetInstallationPath().ToNPath(); this.RawVersion = package.GetVersion(); this.Version = null; try { this.Version = new System.Version(package.GetVersion()); } catch { } }
public static VisualStudioInstance GetVisualStudioInstance() { if (VisualStudioInstanceList == null) { VisualStudioInstanceList = new List <VisualStudioInstance>(); try { ISetupConfiguration2 setupConfiguration = new SetupConfigurationClass() as ISetupConfiguration2; if (setupConfiguration != null) { IEnumSetupInstances instanceEnumerator = setupConfiguration.EnumAllInstances(); ISetupInstance2[] instances = new ISetupInstance2[3]; instanceEnumerator.Next(instances.Length, instances, out var instancesFetched); if (instancesFetched == 0) { return(null); } do { for (int i = 0; i < instancesFetched; i++) { VisualStudioInstanceList.Add(new VisualStudioInstance(instances[i])); } instanceEnumerator.Next(instances.Length, instances, out instancesFetched); }while (instancesFetched != 0); } } catch { } } foreach (VisualStudioInstance instance in VisualStudioInstanceList) { if ( instance.HasRequiredDependency && instance.DisplayName.EndsWith("2019", StringComparison.OrdinalIgnoreCase) // HACK ) { return(instance); } } return(null); }
private static IEnumerable <PackageReference> GetPackages(ISetupInstance2 instance) { var references = instance.GetPackages(); if (references != null) { foreach (var reference in instance.GetPackages()) { if (reference != null) { yield return(new PackageReference(reference)); } } } }
public CosmosBuildDefinition( IInnoSetupService innoSetupService, IMSBuildService msBuildService, ISetupInstance2 visualStudioInstance) { _innoSetupService = innoSetupService; _msBuildService = msBuildService; _visualStudioInstance = visualStudioInstance; _cosmosDir = Path.GetFullPath(Directory.GetCurrentDirectory()); if (!Directory.Exists(Path.Combine(_cosmosDir, "source", "Cosmos.Build.Builder"))) { _cosmosDir = Path.GetFullPath(Path.Combine(_cosmosDir, "..", "..", "..", "..", "..")); } }
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 static VisualStudioInstance GetVisualStudioInstance() { if (VisualStudioInstanceList == null) { VisualStudioInstanceList = new List <VisualStudioInstance>(); try { if (new SetupConfigurationClass() is ISetupConfiguration2 setupConfiguration) { IEnumSetupInstances instanceEnumerator = setupConfiguration.EnumAllInstances(); ISetupInstance2[] instances = new ISetupInstance2[3]; instanceEnumerator.Next(instances.Length, instances, out var instancesFetched); if (instancesFetched == 0) { return(null); } do { for (int i = 0; i < instancesFetched; i++) { VisualStudioInstanceList.Add(new VisualStudioInstance(instances[i])); } instanceEnumerator.Next(instances.Length, instances, out instancesFetched); }while (instancesFetched != 0); } } catch { } } foreach (VisualStudioInstance instance in VisualStudioInstanceList) { if (instance.HasRequiredDependency) { return(instance); } } return(null); }
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); }
public static IEnumerable <ISetupInstance2> GetSetupInstances() { SetupConfiguration setupConfiguration; try { setupConfiguration = new SetupConfiguration(); } catch (COMException e) { unchecked { // Return no insances when VS2017 isn't installed. if (e.ErrorCode == (int)REGDB_E_CLASSNOTREG) { yield break; } } throw; } var instanceEnumerator = setupConfiguration.EnumAllInstances(); var instances = new ISetupInstance2[3]; while (true) { int instancesFetched; instanceEnumerator.Next(instances.Length, instances, out instancesFetched); if (instancesFetched == 0) { break; } for (var index = 0; index < instancesFetched; index++) { yield return(instances[index]); } } }
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 string GetMsBuildForVisualStudio2017() { string str; try { IEnumSetupInstances instances = new SetupConfiguration().EnumInstances(); object[] rgelt = new object[1]; while (true) { int num; instances.Next(rgelt.Length, rgelt, out num); if (num == 0) { return(null); } ISetupInstance2 instance = rgelt[0] as ISetupInstance2; if (instance != null) { try { string[] append = new string[] { "MSBuild", "15.0", "bin", "MSBuild.exe" }; NPath path = instance.GetInstallationPath().ToNPath().Combine(append); if (path.FileExists("")) { return(path.ToString()); } } catch { } } } } catch { str = null; } return(str); }
private static IEnumerable <IVisualStudioInfo> DetectNewVisualStudios() { SetupConfiguration configuration; try { configuration = new SetupConfiguration(); } catch (COMException ex) { // class not registered, no VS2017+ installations if ((uint)ex.HResult == 0x80040154) { yield break; } throw; } IEnumSetupInstances e = configuration.EnumAllInstances(); int fetched; ISetupInstance[] instances = new ISetupInstance[1]; do { e.Next(1, instances, out fetched); if (fetched <= 0) { continue; } ISetupInstance2 instance2 = (ISetupInstance2)instances[0]; string filename = Path.Combine(instance2.GetInstallationPath(), @"Common7\IDE\devenv.exe"); if (File.Exists(filename)) { yield return(new VisualStudio2017Info(Version.Parse(instance2.GetInstallationVersion()), instance2.GetDisplayName(), filename)); } }while (fetched > 0); }
private static List <ISetupInstance> GetVisualStudioInstances() { List <ISetupInstance> vsInstances = new(); try { SetupConfiguration setupConfiguration = new(); ISetupConfiguration2 setupConfiguration2 = setupConfiguration; IEnumSetupInstances setupInstances = setupConfiguration2.EnumInstances(); ISetupInstance[] instances = new ISetupInstance[1]; int fetched = 0; do { setupInstances.Next(1, instances, out fetched); if (fetched > 0) { ISetupInstance2 instance = (ISetupInstance2)instances[0]; // .NET Workloads only shipped in 17.0 and later and we should only look at IDE based SKUs // such as community, professional, and enterprise. if (Version.TryParse(instance.GetInstallationVersion(), out Version version) && version.Major >= 17 && s_visualStudioProducts.Contains(instance.GetProduct().GetId())) { vsInstances.Add(instances[0]); } } }while (fetched > 0); } catch (COMException e) when(e.ErrorCode == REGDB_E_CLASSNOTREG) { // Query API not registered, good indication there are no VS installations of 15.0 or later. // Other exceptions are passed through since that likely points to a real error. } return(vsInstances); }
/// <summary> /// Returns the list of <seealso cref="VisualStudioInstance"/> representing the installed /// Visual Studio instances on the machine. /// </summary> /// <param name="lcid"> /// The local id to use for returning internationalized strings. /// </param> /// <param name="includeIncompleteInstances"> /// If true, does all instances, even those that are incomplete. If false, the default, /// does just completed instances. /// </param> /// <param name="includeAllPackages"> /// If true, includes all packages with the returned results. The default is to only /// return the main installation data. /// </param> /// <returns> /// The list of installed instances. An empty list if none are installed. /// </returns> public static IList <VisualStudioInstance> GetInstalledInstances(int lcid = 0, Boolean includeIncompleteInstances = false, Boolean includeAllPackages = false) { List <VisualStudioInstance> resultList = new List <VisualStudioInstance>(); // Grab the config interface and enumerate the instances. var setupConfig = GetSetupConfig(); if (setupConfig != null) { IEnumSetupInstances instanceList = null; if (includeIncompleteInstances == false) { instanceList = setupConfig.EnumInstances(); } else { instanceList = setupConfig.EnumAllInstances(); } // Now it is time to loop! int fetched; ISetupInstance2[] instance = new ISetupInstance2[1]; do { instanceList.Next(1, instance, out fetched); if (fetched > 0) { var filledInstance = FillInInstanceData(instance[0], lcid, includeAllPackages); resultList.Add(filledInstance); } } while (fetched > 0); } return(resultList); }
private static VisualStudioInstance FindVisualStudioInstance() { var setupConfiguration = new SetupConfiguration() as ISetupConfiguration2; var instanceEnumerator = setupConfiguration.EnumAllInstances(); var instances = new ISetupInstance2[3]; instanceEnumerator.Next(instances.Length, instances, out var instancesFetched); if (instancesFetched == 0) { return(null); } do { for (int i = 0; i < instancesFetched; i++) { var instance = new VisualStudioInstance(instances[i]); var state = instances[i].GetState(); var packages = instances[i].GetPackages().Where(package => package.GetId().Contains("Microsoft.Component.MSBuild")); if ( state.HasFlag(InstanceState.Local | InstanceState.Registered | InstanceState.Complete) && packages.Count() > 0 && instance.Version.StartsWith("15.0", StringComparison.OrdinalIgnoreCase) ) { return(instance); } } instanceEnumerator.Next(instances.Length, instances, out instancesFetched); }while (instancesFetched != 0); return(null); }
private static List <ISetupInstance2> GetSetupInstances() { var instances = new List <ISetupInstance2>(); var setupConfiguration = new SetupConfiguration(); var instanceEnumerator = setupConfiguration.EnumInstances(); var setupInstance = new ISetupInstance2[1]; var count = 0; while (true) { instanceEnumerator.Next(1, setupInstance, out count); if (count == 0) { break; } instances.Add(new VisualStudioSetupInstance(setupInstance[0])); } return(instances); }
public VisualStudioDependency(ISetupInstance2 visualStudioInstance) { _visualStudioInstance = visualStudioInstance; }
/// <summary> /// Initializes a new instance of the <see cref="VisualStudioInstance" /> class. /// </summary> /// <param name="instance">The <see cref="ISetupInstance2" /> of the instance.</param> internal VisualStudioInstance(ISetupInstance2 instance) { _instance = instance ?? throw new ArgumentNullException(nameof(instance)); }