Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit f6eccb4

Browse files
Correctly detecting VisualStudio version on 2017 and forward
1 parent 8ca54d7 commit f6eccb4

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/GitHub.Exports/Services/VSServices.cs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.ComponentModel.Composition;
3+
using System.Diagnostics;
34
using System.Globalization;
45
using System.IO;
56
using System.Linq;
7+
using System.Text.RegularExpressions;
68
using GitHub.Logging;
79
using GitHub.VisualStudio;
810
using Microsoft.VisualStudio;
@@ -151,16 +153,42 @@ string GetVSVersion()
151153
var keyPath = String.Format(CultureInfo.InvariantCulture, "{0}\\{1}.{2}_Config\\SplashInfo", RegistryRootKey, version.Major, version.Minor);
152154
try
153155
{
154-
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(keyPath))
156+
if (version.Major == 14)
155157
{
156-
var value = (string)key.GetValue(EnvVersionKey, String.Empty);
157-
if (!String.IsNullOrEmpty(value))
158-
return value;
158+
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(keyPath))
159+
{
160+
var value = (string)key.GetValue(EnvVersionKey, String.Empty);
161+
if (!String.IsNullOrEmpty(value))
162+
return value;
163+
}
164+
}
165+
else
166+
{
167+
var os = serviceProvider.TryGetService<IOperatingSystem>();
168+
var devenv = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
169+
170+
// C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances
171+
var pathToInstallationData = Path.Combine(os.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData),
172+
"Microsoft", "VisualStudio", "Packages", "_Instances");
173+
174+
var regexVersion = new Regex(@"""productDisplayVersion"":""([^""]+)""");
175+
var regexPath = new Regex(@"""installationPath"":""([^""]+)""");
176+
177+
foreach (var dir in os.Directory.EnumerateDirectories(pathToInstallationData))
178+
{
179+
var data = os.File.ReadAllText(Path.Combine(dir, "state.json"), System.Text.Encoding.UTF8);
180+
if (regexPath.IsMatch(data) && regexVersion.IsMatch(data))
181+
{
182+
var path = regexPath.Match(data).Groups[1].Value;
183+
path = path.Replace("\\\\", "\\");
184+
if (devenv.StartsWith(path, StringComparison.OrdinalIgnoreCase))
185+
{
186+
var value = regexVersion.Match(data).Groups[1].Value;
187+
return value;
188+
}
189+
}
190+
}
159191
}
160-
// fallback to poking the CommonIDE assembly, which most closely follows the advertised version.
161-
var asm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName.StartsWith("Microsoft.VisualStudio.CommonIDE", StringComparison.OrdinalIgnoreCase));
162-
if (asm != null)
163-
return asm.GetName().Version.ToString();
164192
}
165193
catch(Exception ex)
166194
{

0 commit comments

Comments
 (0)