예제 #1
0
        /// <summary>
        /// Get the first Willow VS installation with the specified major vsVersion.
        /// </summary>
        private static bool TryGetEditorInfoWillow(int majorVersion, out Version vsVersion, out string directory)
        {
            Debug.Assert(majorVersion >= 15);

            var setup = new SetupConfiguration();
            var e     = setup.EnumAllInstances();
            var array = new ISetupInstance[] { null };

            do
            {
                e.Next(array.Length, array, out int found);
                if (found == 0)
                {
                    break;
                }

                var instance = array[0];
                if (Version.TryParse(instance.GetInstallationVersion(), out vsVersion) &&
                    vsVersion.Major == majorVersion)
                {
                    directory = Path.Combine(instance.GetInstallationPath(), @"Common7\IDE");
                    return(true);
                }
            }while (true);

            directory = null;
            vsVersion = default;
            return(false);
        }
예제 #2
0
            static IEnumerable <SetupInstance> Get()
            {
                ISetupConfiguration2 cfg;

                try
                {
                    cfg = new SetupConfiguration();
                }
                catch
                {
                    yield break;
                }

                var e = cfg.EnumAllInstances();
                int fetched;
                var instances = new ISetupInstance[1];

                do
                {
                    e.Next(1, instances, out fetched);
                    if (fetched > 0)
                    {
                        yield return(new SetupInstance(instances[0]));
                    }
                } while (fetched > 0);
            }
예제 #3
0
        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]);
        }
예제 #4
0
        private static IEnumerable <ISetupInstance> EnumerateVisualStudioInstances()
        {
            var setupConfiguration = new SetupConfiguration() as ISetupConfiguration2;

            var instanceEnumerator = setupConfiguration.EnumAllInstances();
            var instances          = new ISetupInstance[3];

            var instancesFetched = 0;

            instanceEnumerator.Next(instances.Length, instances, out instancesFetched);

            if (instancesFetched == 0)
            {
                throw new Exception("There were no instances of Visual Studio 15.0 or later found.");
            }

            do
            {
                for (var index = 0; index < instancesFetched; index++)
                {
                    yield return(instances[index]);
                }

                instanceEnumerator.Next(instances.Length, instances, out instancesFetched);
            }while (instancesFetched != 0);
        }
예제 #5
0
        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);
        }
예제 #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("{");

            try
            {
                var config    = new SetupConfiguration();
                var instances = config.EnumAllInstances();

                Console.WriteLine("\"visualstudio\": {");

                int fetched;
                int count  = 0;
                var setups = new ISetupInstance[1];
                do
                {
                    instances.Next(1, setups, out fetched);
                    if (fetched > 0)
                    {
                        if (count > 0)
                        {
                            Console.WriteLine(",");
                        }

                        var instance = (ISetupInstance2)setups[0];

                        Console.Write("\t\"" + SanitizeForJSON(instance.GetDisplayName()) + "\": {");
                        Console.Write("\n\t\t\"version\": \"" + SanitizeForJSON(instance.GetDisplayName()) + "\",");
                        Console.Write("\n\t\t\"supported\": \"" + (instance.IsComplete() ? "true" : "false") + "\",");
                        Console.Write("\n\t\t\"msbuildVersion\": \"" + instance.GetInstallationVersion() + "\",");
                        Console.Write("\n\t\t\"path\": \"" + SanitizeForJSON(instance.GetInstallationPath().Replace("\\", "\\\\")) + "\"");
                        Console.Write("\n\t}");
                        count++;
                    }
                }while (fetched > 0);

                Console.WriteLine("\n}");
            }
            catch (Exception e)
            {
                if (e.HResult != REGDB_E_CLASSNOTREG)
                {
                    Console.Write("\"issues\": [");
                    Console.Write("\n\t{");
                    Console.Write("\n\t\t\"id\": \"WINDOWS_VISUALSTUDIO_2017_DETECT\",");
                    Console.Write("\n\t\t\"type\": \"error\",");
                    Console.Write("\n\t\t\"message\": \"" + SanitizeForJSON(e.Message) + "\"");
                    Console.Write("\n\t}");
                    Console.WriteLine("\n]");
                }
                else
                {
                    Console.Write("\"issues\": []");
                }
            }
            Console.WriteLine("}");
        }
예제 #7
0
        private static string GetMsBuildPath()
        {
            try
            {
                var vsConfiguration       = new SetupConfiguration();
                var vsInstancesEnumerator = vsConfiguration.EnumAllInstances();
                int fetched;
                var vsInstances = new ISetupInstance[1];
                do
                {
                    vsInstancesEnumerator.Next(1, vsInstances, out fetched);
                    if (fetched > 0)
                    {
                        var vsInstance = vsInstances[0];
                        if (vsInstance.GetInstallationVersion().StartsWith("17.")) // Visual Studio 2022
                        {
                            return(vsInstance.ResolvePath(@"Msbuild\Current\Bin\amd64\MSBuild.exe"));
                        }
                    }
                }while (fetched > 0);
            }
            catch (Exception)
            {
            }

            string msBuildPath;

            var programFilesDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);

            foreach (var version in new[] { "14.0", "12.0" })
            {
                var buildDirectory = Path.Combine(programFilesDirectory, "MSBuild", version, "Bin");
                msBuildPath = Path.Combine(buildDirectory, "msbuild.exe");

                if (File.Exists(msBuildPath))
                {
                    return(msBuildPath);
                }
            }

            var netFx = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();

            msBuildPath = Path.Combine(netFx, "msbuild.exe");
            if (!File.Exists(msBuildPath))
            {
                Assert.Fail("Could not find MSBuild at: " + msBuildPath);
            }

            return(msBuildPath);
        }
예제 #8
0
        private static string LocateVSTestEngine()
        {
            bool foundVSTest = false;

            var query = new SetupConfiguration();

            var enumInstances = query.EnumAllInstances();

            while (true)
            {
                int count;
                var instances = new ISetupInstance[1];
                enumInstances.Next(1, instances, out count);
                if (count == 0)
                {
                    break;
                }
                string vspath = instances[0].GetInstallationPath();
                if (!String.IsNullOrEmpty(vspath))
                {
                    string vstest = Path.Combine(vspath, StringResource.VSTestLocation);
                    if (File.Exists(vstest))
                    {
                        // Found test engine.
                        foundVSTest = true;

                        string htmltestlogger = Path.Combine(vspath, StringResource.HtmlTestLoggerLocation);
                        if (File.Exists(htmltestlogger))
                        {
                            // Found html test logger.
                            return(vstest);
                        }
                    }
                }
            }

            if (foundVSTest == false)
            {
                // vstest Not found.
                throw new Exception(StringResource.VSTestNotInstalled);
            }
            else
            {
                throw new Exception(StringResource.HtmlTestLoggerNotInstalled);
            }
        }
예제 #9
0
        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 string[] GetInstancePaths()
        {
            var instancePaths = new List <string>();

            var query = new SetupConfiguration();
            var e     = query.EnumAllInstances();
            int fetched;

            var instances = new ISetupInstance[1];

            do
            {
                e.Next(1, instances, out fetched);
                if (fetched > 0)
                {
                    instancePaths.Add(instances[0].GetInstallationPath());
                }
            } while (fetched > 0);

            return(instancePaths.ToArray());
        }
예제 #11
0
        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 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);
        }
예제 #13
0
        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);
        }
예제 #14
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);
        }
예제 #15
0
        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);
        }
예제 #16
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 < 15)
                        {
                            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})";
                            }
                            else
                            {
                                var installationName = inst2.GetInstallationName();
                                // In case of Preview, we have:
                                // "installationName": "VisualStudioPreview/16.4.0-pre.6.0+29519.161"
                                // "channelId": "VisualStudio.16.Preview"
                                if (installationName.Contains("Preview"))
                                {
                                    displayName = displayName + " (Preview)";
                                }
                            }
                        }
                        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);
        }
예제 #17
0
        /// <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);
                                }
                            }
                        }
                    }