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

Commit 18496f4

Browse files
committed
Consolidate RegistryHelper for all versions
Use the same RegistryHelper for all Visual Studio versions.
1 parent 0a6a0e7 commit 18496f4

File tree

3 files changed

+35
-163
lines changed

3 files changed

+35
-163
lines changed

src/GitHub.TeamFoundation.14/RegistryHelper.cs

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Globalization;
45
using System.IO;
56
using System.Linq;
@@ -14,50 +15,42 @@ namespace GitHub.TeamFoundation
1415
internal class RegistryHelper
1516
{
1617
static readonly ILogger log = LogManager.ForContext<RegistryHelper>();
17-
const string TEGitKey = @"Software\Microsoft\VisualStudio\14.0\TeamFoundation\GitSourceControl";
18+
1819
static RegistryKey OpenGitKey(string path)
1920
{
20-
return Microsoft.Win32.Registry.CurrentUser.OpenSubKey(TEGitKey + "\\" + path, true);
21+
var keyName = $"Software\\Microsoft\\VisualStudio\\{MajorVersion}.0\\TeamFoundation\\GitSourceControl\\{path}";
22+
return Registry.CurrentUser.OpenSubKey(keyName, true);
2123
}
2224

2325
internal static IEnumerable<ILocalRepositoryModel> PokeTheRegistryForRepositoryList()
2426
{
25-
var key = OpenGitKey("Repositories");
26-
27-
if (key != null)
27+
using (var key = OpenGitKey("Repositories"))
2828
{
29-
using (key)
29+
if (key == null)
3030
{
31-
return key.GetSubKeyNames().Select(x =>
32-
{
33-
var subkey = key.OpenSubKey(x);
31+
return Enumerable.Empty<ILocalRepositoryModel>();
32+
}
3433

35-
if (subkey != null)
34+
return key.GetSubKeyNames().Select(x =>
35+
{
36+
using (var subkey = key.OpenSubKey(x))
37+
{
38+
try
3639
{
37-
using (subkey)
38-
{
39-
try
40-
{
41-
var path = subkey?.GetValue("Path") as string;
42-
if (path != null && Directory.Exists(path))
43-
return new LocalRepositoryModel(path, GitService.GitServiceHelper);
44-
}
45-
catch (Exception)
46-
{
47-
// no sense spamming the log, the registry might have ton of stale things we don't care about
48-
}
49-
50-
}
40+
var path = subkey?.GetValue("Path") as string;
41+
if (path != null && Directory.Exists(path))
42+
return new LocalRepositoryModel(path, GitService.GitServiceHelper);
43+
}
44+
catch (Exception)
45+
{
46+
// no sense spamming the log, the registry might have ton of stale things we don't care about
5147
}
52-
5348
return null;
54-
})
55-
.Where(x => x != null)
56-
.ToList();
57-
}
49+
}
50+
})
51+
.Where(x => x != null)
52+
.ToList();
5853
}
59-
60-
return new ILocalRepositoryModel[0];
6154
}
6255

6356
internal static string PokeTheRegistryForLocalClonePath()
@@ -68,23 +61,24 @@ internal static string PokeTheRegistryForLocalClonePath()
6861
}
6962
}
7063

71-
const string NewProjectDialogKeyPath = @"Software\Microsoft\VisualStudio\14.0\NewProjectDialog";
7264
const string MRUKeyPath = "MRUSettingsLocalProjectLocationEntries";
7365
internal static string SetDefaultProjectPath(string path)
7466
{
67+
var newProjectDialogKeyPath = $"Software\\Microsoft\\VisualStudio\\{MajorVersion}.0\\NewProjectDialog";
68+
7569
var old = String.Empty;
7670
try
7771
{
78-
var newProjectKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(NewProjectDialogKeyPath, true) ??
79-
Microsoft.Win32.Registry.CurrentUser.CreateSubKey(NewProjectDialogKeyPath);
72+
var newProjectKey = Registry.CurrentUser.OpenSubKey(newProjectDialogKeyPath, true) ??
73+
Registry.CurrentUser.CreateSubKey(newProjectDialogKeyPath);
8074

8175
if (newProjectKey == null)
8276
{
8377
throw new GitHubLogicException(
8478
string.Format(
8579
CultureInfo.CurrentCulture,
8680
"Could not open or create registry key '{0}'",
87-
NewProjectDialogKeyPath));
81+
newProjectDialogKeyPath));
8882
}
8983

9084
using (newProjectKey)
@@ -132,5 +126,8 @@ internal static string SetDefaultProjectPath(string path)
132126
}
133127
return old;
134128
}
129+
130+
// Major version number of the current devenv process
131+
static int MajorVersion => Process.GetCurrentProcess().MainModule.FileVersionInfo.FileMajorPart;
135132
}
136133
}

src/GitHub.TeamFoundation.15/GitHub.TeamFoundation.15.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@
179179
<Compile Include="..\GitHub.TeamFoundation.14\Home\ForkNavigationItem.cs">
180180
<Link>Home\ForkNavigationItem.cs</Link>
181181
</Compile>
182+
<Compile Include="..\GitHub.TeamFoundation.14\RegistryHelper.cs">
183+
<Link>RegistryHelper.cs</Link>
184+
</Compile>
182185
<Compile Include="..\GitHub.TeamFoundation.14\Services\LocalRepositoryModelFactory.cs">
183186
<Link>Services\LocalRepositoryModelFactory.cs</Link>
184187
</Compile>
@@ -248,7 +251,6 @@
248251
<Compile Include="..\GitHub.TeamFoundation.14\Services\VSGitServices.cs">
249252
<Link>Services\VSGitServices.cs</Link>
250253
</Compile>
251-
<Compile Include="RegistryHelper.cs" />
252254
<Compile Include="..\common\SolutionInfo.cs">
253255
<Link>Properties\SolutionInfo.cs</Link>
254256
</Compile>

src/GitHub.TeamFoundation.15/RegistryHelper.cs

Lines changed: 0 additions & 127 deletions
This file was deleted.

0 commit comments

Comments
 (0)