Example #1
0
        private static List <IDEInfo> BuildIDEInfos()
        {
            var ideInfos = new List <IDEInfo>();

            // Visual Studio 15.0 (2017) and later
            try
            {
                var configuration = new SetupConfiguration();

                var instances = configuration.EnumAllInstances();
                instances.Reset();
                var inst = new ISetupInstance[1];

                while (true)
                {
                    instances.Next(1, inst, out int pceltFetched);
                    if (pceltFetched <= 0)
                    {
                        break;
                    }

                    try
                    {
                        var inst2 = inst[0] as ISetupInstance2;
                        if (inst2 == null)
                        {
                            continue;
                        }

                        // Only deal with VS2019+
                        if (!Version.TryParse(inst2.GetInstallationVersion(), out var version) ||
                            version.Major < 16)
                        {
                            continue;
                        }

                        var installationPath = inst2.GetInstallationPath();
                        var buildToolsPath   = Path.Combine(installationPath, "MSBuild", "Current", "Bin");
                        if (!Directory.Exists(buildToolsPath))
                        {
                            buildToolsPath = null;
                        }
                        var idePath    = Path.Combine(installationPath, "Common7", "IDE");
                        var devenvPath = Path.Combine(idePath, "devenv.exe");
                        if (!File.Exists(devenvPath))
                        {
                            devenvPath = null;
                        }
                        var vsixInstallerPath = Path.Combine(idePath, "VSIXInstaller.exe");
                        if (!File.Exists(vsixInstallerPath))
                        {
                            vsixInstallerPath = null;
                        }

                        var displayName = inst2.GetDisplayName();
                        // Try to append nickname (if any)
                        try
                        {
                            var nickname = inst2.GetProperties().GetValue("nickname") as string;
                            if (!string.IsNullOrEmpty(nickname))
                            {
                                displayName = $"{displayName} ({nickname})";
                            }
                        }
                        catch (COMException)
                        {
                        }

                        try
                        {
                            var minimumRequiredState = InstanceState.Local | InstanceState.Registered;
                            if ((inst2.GetState() & minimumRequiredState) != minimumRequiredState)
                            {
                                continue;
                            }
                        }
                        catch (COMException)
                        {
                            continue;
                        }

                        var ideInfo = new IDEInfo(version, displayName, installationPath, inst2.IsComplete())
                        {
                            BuildToolsPath       = buildToolsPath,
                            DevenvPath           = devenvPath,
                            VsixInstallerVersion = VSIXInstallerVersion.VS2019AndFutureVersions,
                            VsixInstallerPath    = vsixInstallerPath,
                        };

                        // Fill packages
                        foreach (var package in inst2.GetPackages())
                        {
                            ideInfo.PackageVersions[package.GetId()] = package.GetVersion();
                        }

                        ideInfos.Add(ideInfo);
                    }
                    catch (Exception)
                    {
                        // Something might have happened inside Visual Studio Setup code (had FileNotFoundException in GetInstallationPath() for example)
                        // Let's ignore this instance
                    }
                }
            }
            catch (COMException comException) when(comException.HResult == REGDB_E_CLASSNOTREG)
            {
                // COM is not registered. Assuming no instances are installed.
            }
            return(ideInfos);
        }
Example #2
0
        private static List <IDEInfo> BuildIDEInfos()
        {
            var ideInfos = new List <IDEInfo>();

            // Visual Studio 14.0 (2015)
            var localMachine32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);

            using (var subkey = localMachine32.OpenSubKey($@"SOFTWARE\Microsoft\{"VisualStudio"}\{"14.0"}"))
            {
                var path = (string)subkey?.GetValue("InstallDir");

                var vs14InstallPath = (path != null) ? Path.Combine(path, "devenv.exe") : null;
                if (vs14InstallPath != null && File.Exists(vs14InstallPath))
                {
                    var vsixInstallerPath = Path.Combine(path, "VSIXInstaller.exe");
                    if (!File.Exists(vsixInstallerPath))
                    {
                        vsixInstallerPath = null;
                    }

                    ideInfos.Add(new IDEInfo("14.0", "Visual Studio 2015", path)
                    {
                        DevenvPath = vs14InstallPath, VsixInstallerVersion = VSIXInstallerVersion.VS2015, VsixInstallerPath = vsixInstallerPath
                    });
                }
            }

            // Visual Studio 15.0 (2017) and later
            try
            {
                var configuration = new SetupConfiguration();

                var instances = configuration.EnumAllInstances();
                instances.Reset();
                var inst = new ISetupInstance[1];

                while (true)
                {
                    instances.Next(1, inst, out int pceltFetched);
                    if (pceltFetched <= 0)
                    {
                        break;
                    }

                    try
                    {
                        var inst2 = inst[0] as ISetupInstance2;
                        if (inst2 == null)
                        {
                            continue;
                        }

                        var installationPath = inst2.GetInstallationPath();
                        var buildToolsPath   = Path.Combine(installationPath, "MSBuild", "15.0", "Bin");
                        if (!Directory.Exists(buildToolsPath))
                        {
                            buildToolsPath = null;
                        }
                        var idePath    = Path.Combine(installationPath, "Common7", "IDE");
                        var devenvPath = Path.Combine(idePath, "devenv.exe");
                        if (!File.Exists(devenvPath))
                        {
                            devenvPath = null;
                        }
                        var vsixInstallerPath = Path.Combine(idePath, "VSIXInstaller.exe");
                        if (!File.Exists(vsixInstallerPath))
                        {
                            vsixInstallerPath = null;
                        }

                        var displayName = inst2.GetDisplayName();
                        // Try to append nickname (if any)
                        try
                        {
                            var nickname = inst2.GetProperties().GetValue("nickname") as string;
                            if (!string.IsNullOrEmpty(nickname))
                            {
                                displayName = $"{displayName} ({nickname})";
                            }
                        }
                        catch (COMException)
                        {
                        }

                        try
                        {
                            var minimumRequiredState = InstanceState.Local | InstanceState.Registered;
                            if ((inst2.GetState() & minimumRequiredState) != minimumRequiredState)
                            {
                                continue;
                            }
                        }
                        catch (COMException)
                        {
                            continue;
                        }

                        var ideInfo = new IDEInfo(inst2.GetInstallationVersion(), displayName, installationPath, inst2.IsComplete())
                        {
                            BuildToolsPath       = buildToolsPath,
                            DevenvPath           = devenvPath,
                            VsixInstallerVersion = VSIXInstallerVersion.VS2017AndFutureVersions,
                            VsixInstallerPath    = vsixInstallerPath
                        };

                        // Fill packages
                        foreach (var package in inst2.GetPackages())
                        {
                            ideInfo.PackageVersions[package.GetId()] = package.GetVersion();
                        }

                        ideInfos.Add(ideInfo);
                    }
                    catch (Exception)
                    {
                        // Something might have happened inside Visual Studio Setup code (had FileNotFoundException in GetInstallationPath() for example)
                        // Let's ignore this instance
                    }
                }
            }
            catch (COMException comException) when(comException.HResult == REGDB_E_CLASSNOTREG)
            {
                // COM is not registered. Assuming no instances are installed.
            }
            return(ideInfos);
        }