|
1 | 1 | using System; |
2 | 2 | using System.ComponentModel.Composition; |
| 3 | +using System.Diagnostics; |
3 | 4 | using System.Globalization; |
4 | 5 | using System.IO; |
5 | 6 | using System.Linq; |
| 7 | +using System.Text.RegularExpressions; |
6 | 8 | using GitHub.Logging; |
7 | 9 | using GitHub.VisualStudio; |
8 | 10 | using Microsoft.VisualStudio; |
@@ -151,16 +153,42 @@ string GetVSVersion() |
151 | 153 | var keyPath = String.Format(CultureInfo.InvariantCulture, "{0}\\{1}.{2}_Config\\SplashInfo", RegistryRootKey, version.Major, version.Minor); |
152 | 154 | try |
153 | 155 | { |
154 | | - using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(keyPath)) |
| 156 | + if (version.Major == 14) |
155 | 157 | { |
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 | + } |
159 | 191 | } |
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(); |
164 | 192 | } |
165 | 193 | catch(Exception ex) |
166 | 194 | { |
|
0 commit comments