private static string GetMsBuildPathFromRegistry(MsBuildToolsVersion toolsVersion, bool use64Bit)
        {
            const string valueName32Bit = "MSBuildToolsPath32";
            const string valueName64Bit = "MSBuildToolsPath";

            using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\MSBuild\ToolsVersions\" + toolsVersion.Version))
            {
                if (key == null)
                {
                    throw new ArgumentException(
                              string.Format("For the given tools version '{0}' no registry key could be found.", toolsVersion),
                              "toolsVersion");
                }

                var valueName = use64Bit ? valueName64Bit : valueName32Bit;
                var value     = (string)key.GetValue(valueName, null);

                while (value != null && value.StartsWith("$(Registry:") && value.EndsWith(")"))
                {
                    var path = value.Substring(11, value.Length - 12).Split('@');
                    value = (string)Registry.GetValue(path[0], path[1], null);
                }

                if (use64Bit && value == null)
                {
                    value = ((string)key.GetValue(valueName64Bit, null)).Replace("64", "");
                }

                Debug.Assert(value != null, "value != null");
                return(Path.Combine(value, "MSBuild.exe"));
            }
        }
Exemple #2
0
        public static FileInfo GetMsBuildExe(this MsBuildToolsVersion msBuildToolsVersion)
        {
            var toolsVersion = msBuildToolsVersion.GetDescription();
            var keyName      = string.Format(
                @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\{0}\",
                toolsVersion);
            var directory = Registry.GetValue(
                keyName, "MSBuildToolsPath", null);

            return(new FileInfo(Path.Combine(directory.ToString(), "MSBuild.exe")));
        }
        private static string GetMsBuildPathFromRegistry(MsBuildToolsVersion toolsVersion, bool use64Bit)
        {
            if (toolsVersion == MsBuildToolsVersion.v15_0)
            {
                // VS2017 comes with MsBuild tools version 15 which does not include a registry key
                int arrSize = 10, instanceCount;
                var arr = new ISetupInstance[arrSize];
                new SetupConfiguration().EnumInstances().Next(arrSize, arr, out instanceCount);

                var instance = arr.FirstOrDefault(i => i.GetInstallationVersion().Contains("15"));

                if (instance == null)
                {
                    throw new InvalidOperationException(
                              string.Format("Could not find MSBuild executable for version '{0}'.", toolsVersion));
                }

                var msBuildSubFolder = use64Bit ? @"MSBuild\15.0\Bin\amd64" : @"MSBuild\15.0\Bin\";
                return(Path.Combine(instance.GetInstallationPath(), msBuildSubFolder, "MSBuild.exe"));
            }

            const string valueName32Bit = "MSBuildToolsPath32";
            const string valueName64Bit = "MSBuildToolsPath";

            using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\MSBuild\ToolsVersions\" + toolsVersion.Version))
            {
                if (key == null)
                {
                    throw new ArgumentException(
                              string.Format("For the given tools version '{0}' no registry key could be found.", toolsVersion),
                              "toolsVersion");
                }

                var valueName = use64Bit ? valueName64Bit : valueName32Bit;
                var value     = (string)key.GetValue(valueName, null);

                while (value != null && value.StartsWith("$(Registry:") && value.EndsWith(")"))
                {
                    var path = value.Substring(11, value.Length - 12).Split('@');
                    value = (string)Registry.GetValue(path[0], path[1], null);
                }

                if (use64Bit && value == null)
                {
                    value = ((string)key.GetValue(valueName64Bit, null)).Replace("64", "");
                }

                Debug.Assert(value != null, "value != null");
                return(Path.Combine(value, "MSBuild.exe"));
            }
        }