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

Commit 6d3151a

Browse files
committed
Fix registry access for different VS versions
1 parent 9ba8385 commit 6d3151a

File tree

8 files changed

+220
-93
lines changed

8 files changed

+220
-93
lines changed

src/GitHub.TeamFoundation.14/Connect/GitHubConnectSection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using NullGuard;
2020
using ReactiveUI;
2121
using System.Threading.Tasks;
22+
using GitHub.VisualStudio.UI;
2223

2324
namespace GitHub.VisualStudio.TeamExplorer.Connect
2425
{

src/GitHub.TeamFoundation.14/GitHub.TeamFoundation.14.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
<Reference Include="WindowsBase" />
101101
</ItemGroup>
102102
<ItemGroup>
103-
<Compile Include="Constants.cs" />
103+
<Compile Include="RegistryHelper.cs" />
104104
<Compile Include="Services\VSServices.cs" />
105105
<Compile Include="Settings.cs" />
106106
<Compile Include="Base\EnsureLoggedInSection.cs" />
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Globalization;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using GitHub.Models;
9+
using GitHub.VisualStudio;
10+
using Microsoft.Win32;
11+
12+
namespace GitHub.TeamFoundation
13+
{
14+
internal class RegistryHelper
15+
{
16+
const string TEGitKey = @"Software\Microsoft\VisualStudio\14.0\TeamFoundation\GitSourceControl";
17+
static RegistryKey OpenGitKey(string path)
18+
{
19+
return Microsoft.Win32.Registry.CurrentUser.OpenSubKey(TEGitKey + "\\" + path, true);
20+
}
21+
22+
internal static IEnumerable<ISimpleRepositoryModel> PokeTheRegistryForRepositoryList()
23+
{
24+
using (var key = OpenGitKey("Repositories"))
25+
{
26+
return key.GetSubKeyNames().Select(x =>
27+
{
28+
using (var subkey = key.OpenSubKey(x))
29+
{
30+
try
31+
{
32+
var path = subkey?.GetValue("Path") as string;
33+
if (path != null)
34+
return new SimpleRepositoryModel(path);
35+
}
36+
catch (Exception)
37+
{
38+
// no sense spamming the log, the registry might have ton of stale things we don't care about
39+
}
40+
return null;
41+
}
42+
})
43+
.Where(x => x != null)
44+
.ToList();
45+
}
46+
}
47+
48+
internal static string PokeTheRegistryForLocalClonePath()
49+
{
50+
using (var key = OpenGitKey("General"))
51+
{
52+
return (string)key?.GetValue("DefaultRepositoryPath", string.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
53+
}
54+
}
55+
56+
const string NewProjectDialogKeyPath = @"Software\Microsoft\VisualStudio\14.0\NewProjectDialog";
57+
const string MRUKeyPath = "MRUSettingsLocalProjectLocationEntries";
58+
internal static string SetDefaultProjectPath(string path)
59+
{
60+
var old = String.Empty;
61+
try
62+
{
63+
var newProjectKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(NewProjectDialogKeyPath, true) ??
64+
Microsoft.Win32.Registry.CurrentUser.CreateSubKey(NewProjectDialogKeyPath);
65+
Debug.Assert(newProjectKey != null, string.Format(CultureInfo.CurrentCulture, "Could not open or create registry key '{0}'", NewProjectDialogKeyPath));
66+
67+
using (newProjectKey)
68+
{
69+
var mruKey = newProjectKey.OpenSubKey(MRUKeyPath, true) ??
70+
Microsoft.Win32.Registry.CurrentUser.CreateSubKey(MRUKeyPath);
71+
Debug.Assert(mruKey != null, string.Format(CultureInfo.CurrentCulture, "Could not open or create registry key '{0}'", MRUKeyPath));
72+
73+
using (mruKey)
74+
{
75+
// is this already the default path? bail
76+
old = (string)mruKey.GetValue("Value0", string.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
77+
if (String.Equals(path.TrimEnd('\\'), old.TrimEnd('\\'), StringComparison.CurrentCultureIgnoreCase))
78+
return old;
79+
80+
// grab the existing list of recent paths, throwing away the last one
81+
var numEntries = (int)mruKey.GetValue("MaximumEntries", 5);
82+
var entries = new List<string>(numEntries);
83+
for (int i = 0; i < numEntries - 1; i++)
84+
{
85+
var val = (string)mruKey.GetValue("Value" + i, String.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
86+
if (!String.IsNullOrEmpty(val))
87+
entries.Add(val);
88+
}
89+
90+
newProjectKey.SetValue("LastUsedNewProjectPath", path);
91+
mruKey.SetValue("Value0", path);
92+
// bump list of recent paths one entry down
93+
for (int i = 0; i < entries.Count; i++)
94+
mruKey.SetValue("Value" + (i + 1), entries[i]);
95+
}
96+
}
97+
}
98+
catch (Exception ex)
99+
{
100+
VsOutputLogger.WriteLine(string.Format(CultureInfo.CurrentCulture, "Error setting the create project path in the registry '{0}'", ex));
101+
}
102+
return old;
103+
}
104+
}
105+
}

src/GitHub.TeamFoundation.14/Services/VSServices.cs

Lines changed: 4 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.VisualStudio.Shell.Interop;
1212
using Microsoft.Win32;
1313
using System.Diagnostics;
14+
using GitHub.TeamFoundation;
1415
using Microsoft.TeamFoundation.Git.Controls.Extensibility;
1516
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
1617

@@ -38,7 +39,7 @@ public string GetLocalClonePathFromGitProvider()
3839

3940
try
4041
{
41-
ret = PokeTheRegistryForLocalClonePath();
42+
ret = RegistryHelper.PokeTheRegistryForLocalClonePath();
4243
}
4344
catch (Exception ex)
4445
{
@@ -82,7 +83,7 @@ public IEnumerable<ISimpleRepositoryModel> GetKnownRepositories()
8283
{
8384
try
8485
{
85-
return PokeTheRegistryForRepositoryList();
86+
return RegistryHelper.PokeTheRegistryForRepositoryList();
8687
}
8788
catch (Exception ex)
8889
{
@@ -91,93 +92,9 @@ public IEnumerable<ISimpleRepositoryModel> GetKnownRepositories()
9192
}
9293
}
9394

94-
const string TEGitKey = @"Software\Microsoft\VisualStudio\14.0\TeamFoundation\GitSourceControl";
95-
static RegistryKey OpenGitKey(string path)
96-
{
97-
return Registry.CurrentUser.OpenSubKey(TEGitKey + "\\" + path, true);
98-
}
99-
100-
static IEnumerable<ISimpleRepositoryModel> PokeTheRegistryForRepositoryList()
101-
{
102-
using (var key = OpenGitKey("Repositories"))
103-
{
104-
return key.GetSubKeyNames().Select(x =>
105-
{
106-
using (var subkey = key.OpenSubKey(x))
107-
{
108-
try
109-
{
110-
var path = subkey?.GetValue("Path") as string;
111-
if (path != null)
112-
return new SimpleRepositoryModel(path);
113-
}
114-
catch (Exception)
115-
{
116-
// no sense spamming the log, the registry might have ton of stale things we don't care about
117-
}
118-
return null;
119-
}
120-
})
121-
.Where(x => x != null)
122-
.ToList();
123-
}
124-
}
125-
126-
static string PokeTheRegistryForLocalClonePath()
127-
{
128-
using (var key = OpenGitKey("General"))
129-
{
130-
return (string)key?.GetValue("DefaultRepositoryPath", string.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
131-
}
132-
}
133-
134-
const string NewProjectDialogKeyPath = @"Software\Microsoft\VisualStudio\14.0\NewProjectDialog";
135-
const string MRUKeyPath = "MRUSettingsLocalProjectLocationEntries";
13695
public string SetDefaultProjectPath(string path)
13796
{
138-
var old = String.Empty;
139-
try
140-
{
141-
var newProjectKey = Registry.CurrentUser.OpenSubKey(NewProjectDialogKeyPath, true) ??
142-
Registry.CurrentUser.CreateSubKey(NewProjectDialogKeyPath);
143-
Debug.Assert(newProjectKey != null, string.Format(CultureInfo.CurrentCulture, "Could not open or create registry key '{0}'", NewProjectDialogKeyPath));
144-
145-
using (newProjectKey)
146-
{
147-
var mruKey = newProjectKey.OpenSubKey(MRUKeyPath, true) ??
148-
Registry.CurrentUser.CreateSubKey(MRUKeyPath);
149-
Debug.Assert(mruKey != null, string.Format(CultureInfo.CurrentCulture, "Could not open or create registry key '{0}'", MRUKeyPath));
150-
151-
using (mruKey)
152-
{
153-
// is this already the default path? bail
154-
old = (string)mruKey.GetValue("Value0", string.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
155-
if (String.Equals(path.TrimEnd('\\'), old.TrimEnd('\\'), StringComparison.CurrentCultureIgnoreCase))
156-
return old;
157-
158-
// grab the existing list of recent paths, throwing away the last one
159-
var numEntries = (int)mruKey.GetValue("MaximumEntries", 5);
160-
var entries = new List<string>(numEntries);
161-
for (int i = 0; i < numEntries - 1; i++)
162-
{
163-
var val = (string)mruKey.GetValue("Value" + i, String.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
164-
if (!String.IsNullOrEmpty(val))
165-
entries.Add(val);
166-
}
167-
168-
newProjectKey.SetValue("LastUsedNewProjectPath", path);
169-
mruKey.SetValue("Value0", path);
170-
// bump list of recent paths one entry down
171-
for (int i = 0; i < entries.Count; i++)
172-
mruKey.SetValue("Value" + (i + 1), entries[i]);
173-
}
174-
}
175-
}
176-
catch (Exception ex)
177-
{
178-
VsOutputLogger.WriteLine(string.Format(CultureInfo.CurrentCulture, "Error setting the create project path in the registry '{0}'", ex));
179-
}
180-
return old;
97+
return RegistryHelper.SetDefaultProjectPath(path);
18198
}
18299

183100
public void ActivityLogMessage(string message)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@
100100
<Reference Include="WindowsBase" />
101101
</ItemGroup>
102102
<ItemGroup>
103-
<Compile Include="..\GitHub.TeamFoundation.14\Constants.cs">
104-
<Link>Constants.cs</Link>
105-
</Compile>
106103
<Compile Include="..\GitHub.TeamFoundation.14\Settings.cs">
107104
<Link>Settings.cs</Link>
108105
</Compile>
@@ -163,6 +160,7 @@
163160
<Compile Include="..\GitHub.TeamFoundation.14\Services\VSServices.cs">
164161
<Link>Services\VSServices.cs</Link>
165162
</Compile>
163+
<Compile Include="RegistryHelper.cs" />
166164
<None Include="..\..\script\Key.snk" Condition="$(Buildtype) == 'Internal'">
167165
<Link>Key.snk</Link>
168166
</None>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Globalization;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using GitHub.Models;
9+
using GitHub.VisualStudio;
10+
using Microsoft.Win32;
11+
12+
namespace GitHub.TeamFoundation
13+
{
14+
internal class RegistryHelper
15+
{
16+
const string TEGitKey = @"Software\Microsoft\VisualStudio\15.0\TeamFoundation\GitSourceControl";
17+
static RegistryKey OpenGitKey(string path)
18+
{
19+
return Microsoft.Win32.Registry.CurrentUser.OpenSubKey(TEGitKey + "\\" + path, true);
20+
}
21+
22+
internal static IEnumerable<ISimpleRepositoryModel> PokeTheRegistryForRepositoryList()
23+
{
24+
using (var key = OpenGitKey("Repositories"))
25+
{
26+
return key.GetSubKeyNames().Select(x =>
27+
{
28+
using (var subkey = key.OpenSubKey(x))
29+
{
30+
try
31+
{
32+
var path = subkey?.GetValue("Path") as string;
33+
if (path != null)
34+
return new SimpleRepositoryModel(path);
35+
}
36+
catch (Exception)
37+
{
38+
// no sense spamming the log, the registry might have ton of stale things we don't care about
39+
}
40+
return null;
41+
}
42+
})
43+
.Where(x => x != null)
44+
.ToList();
45+
}
46+
}
47+
48+
internal static string PokeTheRegistryForLocalClonePath()
49+
{
50+
using (var key = OpenGitKey("General"))
51+
{
52+
return (string)key?.GetValue("DefaultRepositoryPath", string.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
53+
}
54+
}
55+
56+
const string NewProjectDialogKeyPath = @"Software\Microsoft\VisualStudio\15.0\NewProjectDialog";
57+
const string MRUKeyPath = "MRUSettingsLocalProjectLocationEntries";
58+
internal static string SetDefaultProjectPath(string path)
59+
{
60+
var old = String.Empty;
61+
try
62+
{
63+
var newProjectKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(NewProjectDialogKeyPath, true) ??
64+
Microsoft.Win32.Registry.CurrentUser.CreateSubKey(NewProjectDialogKeyPath);
65+
Debug.Assert(newProjectKey != null, string.Format(CultureInfo.CurrentCulture, "Could not open or create registry key '{0}'", NewProjectDialogKeyPath));
66+
67+
using (newProjectKey)
68+
{
69+
var mruKey = newProjectKey.OpenSubKey(MRUKeyPath, true) ??
70+
Microsoft.Win32.Registry.CurrentUser.CreateSubKey(MRUKeyPath);
71+
Debug.Assert(mruKey != null, string.Format(CultureInfo.CurrentCulture, "Could not open or create registry key '{0}'", MRUKeyPath));
72+
73+
using (mruKey)
74+
{
75+
// is this already the default path? bail
76+
old = (string)mruKey.GetValue("Value0", string.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
77+
if (String.Equals(path.TrimEnd('\\'), old.TrimEnd('\\'), StringComparison.CurrentCultureIgnoreCase))
78+
return old;
79+
80+
// grab the existing list of recent paths, throwing away the last one
81+
var numEntries = (int)mruKey.GetValue("MaximumEntries", 5);
82+
var entries = new List<string>(numEntries);
83+
for (int i = 0; i < numEntries - 1; i++)
84+
{
85+
var val = (string)mruKey.GetValue("Value" + i, String.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
86+
if (!String.IsNullOrEmpty(val))
87+
entries.Add(val);
88+
}
89+
90+
newProjectKey.SetValue("LastUsedNewProjectPath", path);
91+
mruKey.SetValue("Value0", path);
92+
// bump list of recent paths one entry down
93+
for (int i = 0; i < entries.Count; i++)
94+
mruKey.SetValue("Value" + (i + 1), entries[i]);
95+
}
96+
}
97+
}
98+
catch (Exception ex)
99+
{
100+
VsOutputLogger.WriteLine(string.Format(CultureInfo.CurrentCulture, "Error setting the create project path in the registry '{0}'", ex));
101+
}
102+
return old;
103+
}
104+
}
105+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
namespace GitHub.VisualStudio.Helpers
1+
namespace GitHub.VisualStudio.UI
22
{
3-
internal static class Constants
3+
public static class Constants
44
{
55
public const string NoAngleBracketsErrorMessage = "Failed to parse signature - Neither `name` nor `email` should contain angle brackets chars.";
66
public const int MaxRepositoryNameLength = 100;

src/GitHub.VisualStudio.UI/GitHub.VisualStudio.UI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Compile Include="Base\TeamExplorerGitRepoInfo.cs" />
7979
<Compile Include="Base\TeamExplorerItemBase.cs" />
8080
<Compile Include="Colors.cs" />
81+
<Compile Include="Constants.cs" />
8182
<Compile Include="Resources.Designer.cs">
8283
<DependentUpon>Resources.resx</DependentUpon>
8384
<AutoGen>True</AutoGen>

0 commit comments

Comments
 (0)