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

Commit 078366b

Browse files
committed
Fix setting create project dialog path when keys don't exist
Fixes #147
1 parent b2456d8 commit 078366b

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

src/GitHub.Exports/Services/VSServices.cs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -147,36 +147,48 @@ static string PokeTheRegistryForLocalClonePath()
147147
const string MRUKeyPath = "MRUSettingsLocalProjectLocationEntries";
148148
public string SetDefaultProjectPath(string path)
149149
{
150-
string old;
151-
using (var newProjectKey = Registry.CurrentUser.OpenSubKey(NewProjectDialogKeyPath, true))
150+
var old = String.Empty;
151+
try
152152
{
153-
using (var mruKey = newProjectKey?.OpenSubKey(MRUKeyPath, true))
153+
var newProjectKey = Registry.CurrentUser.OpenSubKey(NewProjectDialogKeyPath, true);
154+
if (newProjectKey == null)
155+
newProjectKey = Registry.CurrentUser.CreateSubKey(NewProjectDialogKeyPath);
156+
157+
using (newProjectKey)
154158
{
159+
var mruKey = newProjectKey.OpenSubKey(MRUKeyPath, true);
155160
if (mruKey == null)
156-
return String.Empty;
157-
158-
// is this already the default path? bail
159-
old = (string)mruKey.GetValue("Value0", string.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
160-
if (String.Equals(path.TrimEnd('\\'), old.TrimEnd('\\'), StringComparison.CurrentCultureIgnoreCase))
161-
return old;
161+
mruKey = Registry.CurrentUser.CreateSubKey(MRUKeyPath);
162162

163-
// grab the existing list of recent paths, throwing away the last one
164-
var numEntries = (int)mruKey.GetValue("MaximumEntries", 5);
165-
var entries = new List<string>(numEntries);
166-
for (int i = 0; i < numEntries - 1; i++)
163+
using (mruKey)
167164
{
168-
var val = (string)mruKey.GetValue("Value" + i, String.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
169-
if (!String.IsNullOrEmpty(val))
170-
entries.Add(val);
171-
}
165+
// is this already the default path? bail
166+
old = (string)mruKey.GetValue("Value0", string.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
167+
if (String.Equals(path.TrimEnd('\\'), old.TrimEnd('\\'), StringComparison.CurrentCultureIgnoreCase))
168+
return old;
172169

173-
newProjectKey.SetValue("LastUsedNewProjectPath", path);
174-
mruKey.SetValue("Value0", path);
175-
// bump list of recent paths one entry down
176-
for (int i = 0; i < entries.Count; i++)
177-
mruKey.SetValue("Value" + (i+1), entries[i]);
170+
// grab the existing list of recent paths, throwing away the last one
171+
var numEntries = (int)mruKey.GetValue("MaximumEntries", 5);
172+
var entries = new List<string>(numEntries);
173+
for (int i = 0; i < numEntries - 1; i++)
174+
{
175+
var val = (string)mruKey.GetValue("Value" + i, String.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
176+
if (!String.IsNullOrEmpty(val))
177+
entries.Add(val);
178+
}
179+
180+
newProjectKey.SetValue("LastUsedNewProjectPath", path);
181+
mruKey.SetValue("Value0", path);
182+
// bump list of recent paths one entry down
183+
for (int i = 0; i < entries.Count; i++)
184+
mruKey.SetValue("Value" + (i + 1), entries[i]);
185+
}
178186
}
179187
}
188+
catch (Exception ex)
189+
{
190+
VsOutputLogger.WriteLine(string.Format(CultureInfo.CurrentCulture, "Error setting the create project path in the registry '{0}'", ex));
191+
}
180192
return old;
181193
}
182194

0 commit comments

Comments
 (0)