public static string GetVisualStudioDir(this DevEnv visualVersion, bool ignoreVisualStudioPathOverride = false) { // TODO: Replace Tuple with ValueTuple once we support C# 8 because ValueTuple is // allocated on the stack. That should be faster here. string visualStudioDirectory = s_visualStudioDirectories.GetOrAdd(Tuple.Create(visualVersion, ignoreVisualStudioPathOverride), devEnv => { // First check if the visual studio path is overriden from default value. string pathOverride; if (s_visualStudioDirOverrides.TryGetValue(visualVersion, out pathOverride)) { return(pathOverride); } string installDir = Util.GetVisualStudioInstallPathFromQuery(visualVersion); if (string.IsNullOrEmpty(installDir)) { switch (visualVersion) { case DevEnv.vs2017: case DevEnv.vs2019: installDir = @"Microsoft Visual Studio\" + GetVSYear(visualVersion) + @"\Professional"; break; default: installDir = string.Format(@"Microsoft Visual Studio {0}", visualVersion.GetVisualVersionString()); break; } installDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), installDir); } return(Util.SimplifyPath(installDir)); }); return(visualStudioDirectory); }
public static string GetVisualStudioDir(this DevEnv visualVersion, bool ignoreVisualStudioPathOverride = false) { if (!visualVersion.IsVisualStudio()) { throw new Error($"{visualVersion} is not a visual studio version!"); } // TODO: Replace Tuple with ValueTuple once we support C# 8 because ValueTuple is // allocated on the stack. That should be faster here. string visualStudioDirectory = s_visualStudioDirectories.GetOrAdd(Tuple.Create(visualVersion, ignoreVisualStudioPathOverride), devEnv => { if (!ignoreVisualStudioPathOverride) { // First check if the visual studio path is overridden from default value. string pathOverride; if (s_visualStudioDirOverrides.TryGetValue(visualVersion, out pathOverride)) { return(pathOverride); } } string installDir = Util.GetVisualStudioInstallPathFromQuery(visualVersion); if (string.IsNullOrEmpty(installDir)) { // try again but this time including previews installDir = Util.GetVisualStudioInstallPathFromQuery(visualVersion, allowPrereleaseVersions: true); if (string.IsNullOrEmpty(installDir)) // arbitrary fallback { if (visualVersion > DevEnv.vs2015) { installDir = @"Microsoft Visual Studio\" + GetVSYear(visualVersion) + @"\Professional"; } else { installDir = string.Format(@"Microsoft Visual Studio {0}", visualVersion.GetVisualVersionString()); } string rootDir = Environment.GetFolderPath(visualVersion < DevEnv.vs2022 ? Environment.SpecialFolder.ProgramFilesX86 : Environment.SpecialFolder.ProgramFiles); installDir = Path.Combine(rootDir, installDir); } } return(Util.SimplifyPath(installDir)); }); return(visualStudioDirectory); }