11using System ;
22using System . Collections . Generic ;
3+ using System . Diagnostics ;
34using System . Globalization ;
45using System . IO ;
56using 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}
0 commit comments