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

Commit 7c2b795

Browse files
Using SetupConfiguration to get the version in VisualStudio 15 and above
1 parent f6eccb4 commit 7c2b795

File tree

3 files changed

+11
-24
lines changed

3 files changed

+11
-24
lines changed

src/GitHub.Exports/GitHub.Exports.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
<HintPath>..\..\packages\Microsoft.VisualStudio.ComponentModelHost.14.0.25424\lib\net45\Microsoft.VisualStudio.ComponentModelHost.dll</HintPath>
6464
<Private>True</Private>
6565
</Reference>
66+
<Reference Include="Microsoft.VisualStudio.Setup.Configuration.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
67+
<HintPath>..\..\packages\Microsoft.VisualStudio.Setup.Configuration.Interop.1.15.103\lib\net35\Microsoft.VisualStudio.Setup.Configuration.Interop.dll</HintPath>
68+
<EmbedInteropTypes>True</EmbedInteropTypes>
69+
</Reference>
6670
<Reference Include="Microsoft.VisualStudio.Shell.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
6771
<HintPath>..\..\packages\Microsoft.VisualStudio.Shell.14.0.14.3.25407\lib\Microsoft.VisualStudio.Shell.14.0.dll</HintPath>
6872
<Private>True</Private>

src/GitHub.Exports/Services/VSServices.cs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using GitHub.Logging;
99
using GitHub.VisualStudio;
1010
using Microsoft.VisualStudio;
11+
using Microsoft.VisualStudio.Setup.Configuration;
12+
using Microsoft.VisualStudio.Shell;
1113
using Microsoft.VisualStudio.Shell.Interop;
1214
using Rothko;
1315
using Serilog;
@@ -147,6 +149,7 @@ void TryCleanupSolutionUserFiles(IOperatingSystem os, string repoPath, string sl
147149

148150
const string RegistryRootKey = @"Software\Microsoft\VisualStudio";
149151
const string EnvVersionKey = "EnvVersion";
152+
const string InstallationNamePrefix = "VisualStudio/";
150153
string GetVSVersion()
151154
{
152155
var version = typeof(Microsoft.VisualStudio.Shell.ActivityLog).Assembly.GetName().Version;
@@ -164,30 +167,9 @@ string GetVSVersion()
164167
}
165168
else
166169
{
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-
}
170+
var setupConfiguration = new SetupConfiguration();
171+
var setupInstance = setupConfiguration.GetInstanceForCurrentProcess();
172+
return setupInstance.GetInstallationName().TrimPrefix(InstallationNamePrefix);
191173
}
192174
}
193175
catch(Exception ex)

src/GitHub.Exports/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<package id="LibGit2Sharp.NativeBinaries" version="1.0.164" targetFramework="net461" />
55
<package id="Microsoft.VisualStudio.ComponentModelHost" version="14.0.25424" targetFramework="net461" />
66
<package id="Microsoft.VisualStudio.OLE.Interop" version="7.10.6070" targetFramework="net461" />
7+
<package id="Microsoft.VisualStudio.Setup.Configuration.Interop" version="1.15.103" targetFramework="net461" developmentDependency="true" />
78
<package id="Microsoft.VisualStudio.Shell.14.0" version="14.3.25407" targetFramework="net461" />
89
<package id="Microsoft.VisualStudio.Shell.Immutable.10.0" version="10.0.30319" targetFramework="net461" />
910
<package id="Microsoft.VisualStudio.Shell.Interop" version="7.10.6071" targetFramework="net461" />

0 commit comments

Comments
 (0)